Hello, I am wiring a vertical lift bridge system for my students and am unsure of the circuit I have built. I have 4 dc motors with transistors and diodes working off 1 push button and the arduino. The code I have seems to be fine and the arduino is happy with it, but the mechanics won't work.
These are the circuits I combined to run the motors in series:

This is the code I have:
const int buttonPin = 2; //Change this pin as per your wiring
const int motorPins[] = {6,7,8,9}; // Change this pin as per your wiring
const int numMotors = 4;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
// put your setup code here, to run once:
for (int i = 0; i <numMotors; i++) {
pinMode (motorPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead (buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
//Toggle the state of all motors
for (int i = 0; i < numMotors; i++) {
digitalWrite (motorPins[i], !digitalRead(motorPins[i]));
}
}
delay (50); //Debounce delay
}
Pictures of the circuit



I followed those guidelines when I created the post, I do not know why it displayed incorrectly. For someone who is new to the forum seeking help and guidance a little leeway and grace should be given.
The "won't work" means the code seems fine, and downloaded to the arduino okay, but the motors will not turn on and spin when I push the button.
Which I did. Instead of ignoring your post and moving on to someone else's problem, because you didn't correctly follow forum guidelines, I took the trouble and time to explain what was wrong and what was missing. I even said "please" 4 times!
I'm a high school engineering teacher trying to incorporate programming into my curriculum. I know a little bit, enough to get things started but not enough to troubleshoot and get things running.
By the time you are done, you will have learnt a variety of troubleshooting methods.
In addition to using copious serial print statements to watch the flow of your code and prove it is being informed by the values of key variables and inputs…
a trick for motors is to temporarily remove the motors and replace them with LEDs and series current limiting resistors. LEDs should do what you want the motors to do.
Next see if you can make a motor spin by directly hooking what once went to an output pin on the Arduino to ground and then to Vcc (5 volts).
Motors are often problematic due to power supply inadequacy or dodgy wiring.
By testing the software with LEDs and confirming your transistor circuit with direct injection of the logic voltage you expect the Arduino to put out, you have employed the very best troubleshooting method for these kindsa projects:
I see the diodes connected to ground, which is wrong. The diodes go across the motor, not across the transistor. The transistors are hopefully logic level mosfets. Because no base resistors on bjt transistors will burn the Arduino pins. Mosfets should also have (two) resistors.
Better put that 9volt battery back in the smoke alarm. A pack of AA batteries is much better suited to power motors.
Leo..
I have 4 dc motors that I want to program with an arduino to run a vertical lift (bridge elevator essentially). I do not have a motor controller unit so I am using transistors. I need to control this system with a push button to activate and reverse the system. I found the images and code online to get me started but never found close to what I need. I found two images that were close to what I need and tried to integrate them.
I am not a coder, nor do I do much with electronics, as a teacher I don't have a lot of time to tinker with these. I am trying to learn these two new careers in order to teach my students and just need some help and direction.
As per my reply, the single motor circuit looks okay so far as I can see. The 3 motor circuit is wrong, again, so far as I can see. Compare the two, look for the mistakes then do a corrected schematic and post that.