The problems I’m having with Mousey are not unique. Checkout this YouTube video from Makemagazine with Bre Pettis, who had similar problems with the mouse driving in circles and a motor hooked up backwards. Unfortunately, in his brief video he doesn’t go into detail about how he diagnosed and fixed the problems in the circuit. He shows the Mousey not working, driving in circles like mine does, and then Mousey works properly.
This blog entry serves 2 purposes, it forces me to fully think through and write down my troubleshooting steps and test data. Second it might help others who are troubleshooting similar circuits. I know there are some kids and teachers who read my blog and hopefully everyone can learn that problems can be overcome with a series of hypothesis, tests and refined hypothesis. The Scientific Method Rules!
Desired Behavior of Mousey: I want my 2 motors to receive the same power (voltage) when both light sensors receive the same ambient light. If the right sensor receives more light, then I want the left motor to move faster than the right motor, thus moving Mousey toward the light. Vice versa with the right motor and left sensor. Also, when the bump switch is closed briefly (when bumped) it should reverse both motors, which makes the Mousey run backwards like it is afraid for a few seconds before returning to normal forward behavior.
Problem statement 1: Under ambient light, one motor consistently receives more power than the other, which would make my robot drive in circles to the left. One motor gets about 4 VDC, the other gets 4.5 VDC. Half a volt difference will make one motor significantly faster. Note that I don’t have the motors attached right now because I’m isolating any differences in the two motors and focusing on the voltage that each one receives out of the Relay pins 4 and 13. My relay is R72-11D1-5 and the PDF datasheet can be found on the NTE web site here.
Problem statement 2: The bump switch doesn’t cause the motors to reverse. Let’s tackle problem 1 first and get to problem 2 in another blog post.
Mousey Circuit, by Gareth Branwyn
Hypothesis 1: The light sensors are the problem, sending different readings when exposed to the same ambient light.
Test for Hypothesis 1: Check if the light sensors are sending widely different voltages into the Op Amp (LM 386). Place the voltmeter between ground and the anode of each light sensor, noting that the anodes are connected to pins 2 and 3, respectively of the Op Amp (LM386).
light sensor Left (touching anode and cathode of sensor pins): 2V
light sensor Right (touching anode and cathode of sensor pins): 2V
Op Amp pin 2 & GND: 0.0048 V
Op Amp pin 3 & GND: 0.0041 V
In other words, the left sensor is getting more light.
Op Amp pin 5 and GND: 3.94 V
The output of pin 5 goes to the motors and divides the energy
between High (Relay pin 13) and Low (Relay pin 4 = GND = 0)
Transistor base and GND: 0.34 V (same across capacitor)
Transistor collector to emitter, and into pin 16 of Relay: 8.61 V
Relay pin 4 to GND: 0 V
Relay pin 13 to GND: 8.61 V
Left motor = Relay Pin 4 (0V) to OpAmp Pin 5 (3.94V) = -3.94 V
Right Motor = Relay Pin 13 (8.61 V) to OpAmp Pin 5 (3.94V) = 4.67 V
Note that it’s perfectly fine that one motor gets negative and the other positive voltage because the motors will be laying on their sides, and one rotates clockwise, the other counterclockwise to make the mouse go forward.
High voltage from the battery is around 8.6 V and I want the output of OpAmp Pin 5 to be exactly half = 4.3 V so that it equally divides the voltage between the two motors and makes them go the same speed, making Mousey drive straight. I continued testing by metering OpAmp pin 5 and found that I could get 4.3 V when a lot more light goes into the right light sensor than the left, either by putting the left sensor in shade, or putting the flashlight right on top of the right sensor.
Troubleshooting the light sensors: Ideally the sensors behave the same way under the same light conditions. If that were true, I would see the same behavior if I swapped the sensors on my board. Specifically, I would pull the black wire from OpAmp pin 2 and swap it with the black wire from OpAmp pin 3.
Original left sensor to Pin 2: 4.9 mV
Original Right sensor to Pin 3: 4.5 mV, a difference of 0.4 mV
Output of OpAmp pin 5 is 3.94 V (same as above experiment)
Swapped sensor to Pin 2: 5.4 mV
Swapped sensor to Pin 3: 4.1 mV, a difference of 1.3 mV
Output of the OpAmp pin 5 is worse at 3.69 V,
which makes Mousey spin in even tighter circles.
No sensor connected to Pin 2: 3.2 mV
No sensor connected to Pin 3: 2.0 mV
Output OpAmp Pin 5: 3.79 V
What have I learned from these readings? OpAmp input Pin 2 always has a higher voltage than input Pin 3. That doesn’t make sense to me.
Under similar light conditions we want OpAmp output Pin 5 to have an optimal midpoint value of 4.3 V. What I learned from this test of swapping the sensors is that regardless of the sensors, the OpAmp seems to be receiving an input difference between pin 2 and pin 3, and outputting less than half of my Vmax. I would prefer exactly half of Vmax.
Test 2 for Hypothesis 1: Try different sensors
I purchased two IR LEDs (Infrared light emitting diodes) for ten cents each. When I replaced my recycled sensors with these brand new ones, I still saw a difference between the input voltages, and still don’t get the desired 4.3 V output:
New IR LED sensor on OpAmp pin 2 = 8mV
New IR LED sensor on OpAmp pin 3 = 6mV
Output of OpAmp pin 5 = 3.6 V
Is this normal behavior for an OpAmp? Time to research!
Hypothesis 2: Something is wrong with the Op Amp (LM386).
These chips are static sensitive and I could have fried it. I do my work in slippers, on carpet, and a fuzzy cat frequently walks across my desk. Static is everywhere! A few quick tests showed that I did not fry it.
The OpAmp LM386 datasheet is full of information that I don’t completely understand. Pins 1 and 8 are the Gain pins, and the Mousey instructions in my book tells me that wiring them together helps make my circuit more sensitive to changes in the light. So, on a whim, I removed the wire connecting pins 1 and 8 on the OpAmp and Abracadabra I get the exact midpoint 4.3 V output from pin 5! The midpoint voltage means that my motors will move at exactly the same speed. The other thing I really want is for the output of pin 5 to CHANGE when there is a big difference between the amount of light on one sensor vs the other. When I put one sensor in the shade and shine a flashlight on the other sensor, my pin 5 output barely changes. It stays within 20 mV of 4.3 V. I don’t want this behavior. I want more dramatic changes in voltage to really make Mousey turn towards a light source. From this experiment I learned that the gain really does make a more dramatic change in the output of pin 5.
What if I put a resistor instead of a wire between the gain pins? I tried a 1000 Ohm resistor which acted a lot like the circuit did when it was wide open. Then a 510 Ohm, and it still wasn’t sensitive enough. Experiments with smaller resistors gave good results which I will describe below here. When running experiments it is good to keep in mind our Desired Outcomes. Remember a perfect midpoint voltage under ambient light is 4.3 V and a desired change in motor speeds will come from about 200mV of difference between the motors.
With the 200 Ohm resistor across the gain pins, I get close to my midpoint desired voltage. OpAmp pin 5 = 4.2 V, Left Motor = 4.2 V, Right Motor = 4.3V. My light sensitivity test shows that when I put bright light on the Right sensor, the Right motor voltage drops to 4.2V and the Left motor speeds up to 4.36 Volts. I like this combination of nearly equal speeds at ambient light (100 mV difference) and difference of 160 mV in the flashlight test.
With the 100 Ohm resistor across the gain, I now get an ambient light reading from pin 5 of 4.05V giving me 4.05 V on the left motor, 4.46 on the right, which means Mousey will veer to the left under ambient light. Not optimal ambient light behavior. Next I ran the flashlight test. With light on the right sensor, the voltage of the right motor drops to 3.95 V and the Left Motor speeds up to 4.2. The delta in motor voltages is 250 mV in the flashlight test. This circuit is more sensitive than the previous experiment, but the ambient light behavior is undesirable (400 mV difference).
Result: Adding a 200 Ohm resistor across the gain of the OpAmp gives more accurate behavior in ambient light and flashlight conditions. Yeah! To summarize what I learned today, I have a better understanding of the Gain on an OpAmp chip, and how to modify it using resistors to give a desired output.
Mousey Part 3: Add a resistor to the OpAmp Gain
Problem Behavior 2: Bump Switch behavior not working properly.
I pulled the DPDT (double pole double throw) relay off of my breadboard circuit and put it into another breadboard for “bench testing.” A bench test is where you isolate one part of your circuit, in this case the DPDT relay integrated circuit. A relay has a normal state, and a state when a voltage is applied across the coil, in this case pins 1 and 16. It can be confusing reading the data sheet for your relay because each manufacturer might number their pins differently. However all DPDT relays will have two pins for the coil, in our diagram 1 and 16. Then two common pins, usually labeled COM1 and COM2, which are the outputs. There are usually 4 inputs in a DPDT relay, two Normally Open inputs and two Normally Closed inputs, labeled NO1, NO2, NC1, NC2.
DPDT relay with numbered pins
When there is no voltage across the coil, then COM1 voltage = NC1 and COM2 = NC2. When sufficient voltage is across the coil, then the internal circuit of the relay will switch so that COM1 voltage = NO1 and COM2 = NO2. With this data, you can run a quick bench test using either a voltmeter or a couple of LEDs and resistors.
Another example of DPDT diagram from a data sheet
In my test, I discovered that my relay was broken because when I put 8 volts across the coil, the common pins still registered the values of the normally closed pins. In other words, my relay would not switch. So I threw it out and purchased a new one. The new relay passed the bench test, so I put it back in the Mousey circuit and it worked fine! Mousey now drives backwards when the bump switch is pushed, which puts voltage across the coil of the relay, which reverses the polarity of the voltage to the motors. Make sense? Please post questions in the comment section of this blog if you need help with your circuit.