
In this tutorial we will order a DC motor by the Arduino board:
1- When we press the push button, the motor turns
2- When we release the push button, the motor stops.
As for the assembly, we can connect
For relay:
For DC motor:
For push button:
Here is the program that allows to control a DC motor by Arduino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
const int bouton = 1; // the button is connected to pin 1 of the Adruino board const int relais_moteur = 2; // the relay is connected to pin 1 of the Adruino board int etatBouton; void setup() { pinMode(bouton, INPUT); pinMode(relais_moteur, OUTPUT); } void loop() { etatBouton = digitalRead(bouton); if (etatBouton == 0) // Push the push button { digitalWrite(relais_moteur, HIGH); // The motor starts to run } else // Release the push button { digitalWrite(relais_moteur, LOW); // The motor stops running } } |
Stefan 18-11-2121
My DC motor doesn't turn on at all. I did the exact same thing as you. But if I plug in a LED instead of the motor, the LED turns on. Any ideas why? I connected the relay directly to arduino board..