Controlling a DC Motor with a Switch using Arduino UNO

Here's some sample Arduino code for controlling a motor with a switch:

This code uses the Arduino's digitalRead() function to read the state of the switch, and digitalWrite() function to control the motor pin. If the switch is pressed, the motor pin is set to HIGH, which causes the motor to run in forward direction. If the switch is not pressed, the motor pin is set to LOW, which causes the motor to stop.

To use this code, you will need the following electronic components:

  1. An Arduino UNO or any arduino board
  2. A DC motor
  3. A switch
  4. A breadboard
  5. Jumper wires
  6. A power supply for the motor, usually 6 to 12V DC will work depending on the motor.

You will also need to connect the components together in a circuit, here's a simple circuit diagram:

  1. Connect the positive lead of the power supply to one of the power rail on the breadboard, and the negative lead to the other rail.
  2. Connect the positive lead of the motor to the same rail as the positive lead of the power supply, and the negative lead of the motor to the same rail as the negative lead of the power supply.
  3. Connect one end of the switch to the same rail as the positive lead of the power supply, and the other end to a free row on the breadboard.
  4. Connect the free row of the switch to a digital pin on the Arduino board, the code I provided uses pin 2.
  5. Connect the motor control pin (digital pin 9) on the Arduino board to the same row as the negative lead of the motor.
Please make sure that the connections are correct and you're using the right pin numbers as mentioned in the code.

You can buy all the electronic items from amazon or any other online or physical store.

Please let me know if you have any questions or if there's anything else I can help you with.

No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.