I have designed a circuit using the arduino nano which should control two vibration motors. The motors and arudino are all powered by a single 9v battery. When I send a signal to pin 9 of the arduino, it sends an ouput voltage to pins 2 and 3. These pins are connected to NPN transistors which, when they receive a voltage, should open their gates and allow the battery power to turn on the motors. The motors are each connected in parallel with a capacitor and an LED to protect them, as was advised on several forums I read when constructing the circuit. The problem I am having is that sometimes, when I send a signal to pin 9, both motors will turn on, while more often, when I send the signal only the first motor will turn on. I do not believe this is a hardware or power issue, as I have checked all of the hardware and wiring very carefully and, as I mentioned before, the whole circuit works sometimes. I am wondering whether it is possible that the reduced output signal corresponds to a reduced input signal. It is possible that sometimes the signal to pin 9 is a little bit under 5v, while other times it is 5v. Does the arduino output a signal proportional to the input signal? If so, how might I modify my code to correct for this? If not, what might be causing my problem? I have included my code below and have attached a picture of the circuit and motor datasheet.
NOTE: In the attached picture, the battery and motors are missing from the circuit. The battery is connected across the red and black wires on the left (I now have a proper battery connector, so that is not the source of the issue) and each motor is are in parallel to a capacitor and LED.
CODE:
int switchPin = 9;
int val; // variable for storing value of switchPin
void setup() {
// initialize digital pins 02 and 03 as outputs.
pinMode(02, OUTPUT);
pinMode(03, OUTPUT);
// initialize digital pin 09 as input.
pinMode(9, INPUT);
}
void loop() {
val = digitalRead(switchPin);
if (val==HIGH) { // if pin 09 receives a voltage
digitalWrite(02, HIGH); // turn vibration motor 1 on
digitalWrite(03, HIGH); // turn vibration motor 2 on
} else {
digitalWrite(02, LOW); // turn vibration motor 1 off
digitalWrite(03, LOW); // turn vibration motor 2 off
}
}
