I am using an TIP 120 NPN transistor to control a DC motor with its voltage supplied from a 12V power supply. The circuit below shows one motor however I have 5 hooked up all using the same wiring scheme. I am able to control the motors individually (can't run them all at once as there is not enough power), using the analogWrite or DigitalWrite functions, however as soon as I insert a line of code that calls on the analogRead function, the motors stop working and no matter what line of code comes next they don't turn on until I restart the program.
Nothing needs to be even plugged into the Analog inputs, its just as soon as I call that specific function all motors stop working. I have a feeling it has something to do with the wiring of my circuit but I am not experienced enough to know what. For some context, I am controlling 5 DC motors unidirectionally(hence no need for an H bridge), and I have a photo diode connected to the A0 input, but like I said the problem happens whether something is plugged in or not. Hohwever, digital inputs work fine and don't mess with the motor control.
The Circuit I don't have the button seen below attached in my circuit however i've tried and the button works fine, its analog inputs that are the issue
int motor1 = 3;
int motor2 = 5;
int motor3 = 6;
int photoDiode = A0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//COIN DETECTOR
//TRIP DETECTOR
pinMode(photoDiode,INPUT);
//MOTORS
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor3, OUTPUT);
pinMode(8,INPUT);
}
void motorOn(int motorNum){
digitalWrite(motorNum,HIGH);
}
void loop() {
digitalWrite(motor1,HIGH);
delay(1000);
// MOTOR TURNS ON FOR A SECOND UNTIL IT REACHES THE ANALOG READ AND THEN
// NEVER TURNS ON AGAIN
analogRead(photoDiode);
}
MUST use a current limiting resistor between the output pin and base of the TIP120.
You're currently overloading the Arduino pin, which could result in frying the pin.
Resistor value depends on current draw of the motors, but 220ohm should work.
You're not reading the diode value into a variable. Not sure if the compiler likes that.
photodiodeValue = analogRead(photodiodePin);
Leo..
In addition to the resistor Wawa suggested the layout shown is far from ideal.
I am assuming your Fritzing mimics the actual location of the wires on the solderless breadboard. It that is the case you should make some changes.
The solderless breadboards do not make the best connections so it is important to keep low and high currents separate.
For instance the TIP120 ground goes from its pin all the way over to the left side ground then back to the right side ground then the full length of the board back to the battery ground.
You should move the TIP120 over to near the motor keeping all the motor related wires in that upper right corner. Then run only one wire over to the arduino ground.
Looking at the 9V positive wire. It could go right to the motor yellow wire and eliminate the two extra connections and the small red jumper.
If you have any capacitors around you should put a 0.1µF across the battery
digradom:
I am using an TIP 120 NPN transistor to control a DC motor with its voltage supplied from a 12V power supply. The circuit below shows one motor however I have 5 hooked up all using the same wiring scheme.
That's not a schematic; that's Fritzy eye candy and as usual no idea to what extent that resembles reality. For starters you say you use a 12V power supply but draw a 9V battery (of a type utterly unsutiable to run a motor to boot), then you say you don't have a button but it's there, and you say you have an LDR but it's not in the drawing. Your code mentions pins 3, 4, 6, 8 and A0 - none of which have any connection in that image.
That makes the image useless and we're probably better off if you simply remove it, take pencil and paper, draw a schematic diagram of how you DO hjave it hooked up, with all actual components present, and post that. You're wasting our time now (and your own as well, but that's of course quite irrelevant).
Running motor currents through breadboards is an issue; those solderless breadboards can not carry much current, a safe limit is 0.5A.
Wawa:
You're not reading the diode value into a variable. Not sure if the compiler likes that.
photodiodeValue = analogRead(photodiodePin);
In my experience the compiler couldn't care less, the return value is simply ignored in this case. Trying to read a value from a function that does not return one, on the other hand, does give an error. As does trying to send a return value from a function declared void.