DC Motor Control using Arduino UNO and IR Sensor

Naga Surya Praveen E
3 min readJan 6, 2018

--

(This post is about the technical details of my first experiment using Arduino)

Arduino does a great job in introducing electronics and programming to who ever interested in it. If you are interested in electronics and if you can learn basic C programming, you can buy an Arduino without a doubt and start your electronic projects with it.

Most of the learning material is available in their website https://www.arduino.cc/

What is Arduino anyway ?

If you would like to glow a tiny LED bulb, what would you do ? The most basic answer would be — Connect it to a battery.

Ok. Fine. What would you do if you would like to blink the same bulb every 1 second ? Obviously you cannot switch off and switch on the bulb manually. There has to be some controller for this. And this controller must be programmed so that it can switch the power ON and OFF every 1 second. Arduino is this controller.

Write a program. Connect your electronics to it. Provide power supply. Build things.

Here is my old video on how I controlled a motor using my hand. You can notice that if the sensor detects my hand, it will stop the motor.

The sensor doesn’t know what to do when it detects my hand. It just informs Arduino that it detected some object in front of it. Based on this, our program inside Arduino will stop the motor.

You cannot directly connect your motor to any of the ports of Arduino because motors draw more current and your Arduino will burn with this current. So, we use a intermediate chip L293D. Also note that if you connect more than two motors, the IC will burn.

Circuit for controlling a motor with Arduino and IR sensor

In the below step, HIGH and LOW refer to ON and OFF. Arduino supports HIGH and LOW in its language. You can compare this instructions with the program.

  • Connect pin 3 of Arduino to pin 2 of L293
  • Connect pin 4 of Arduino to pin 7 of L293 (If pin3 is HIGH and pin4 is LOW, motor will rotate in one direction. If pin3 is LOW and pin4 is HIGH, motor will rotate in reverse direction. If both the pins are same(either LOW or HIGH), motor will stop)
  • Connect pin 9 of Arduino to pin 1 of L293 (this pin should be always HIGH. If you want to stop the motor, you can set this pin as LOW)
  • In L293, connect pins 4,5,12,13 to ground
  • In L293, connect pin 16 to 5V ( you can power it from Arduino)
  • In L293, give the required voltage of motor to pin 8 connect IR sensor output to A0 pin of Arduino

Code

const int switchPin = 2; // switch input
const int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
void setup() {
// set the switch as an input:
pinMode(switchPin, INPUT);
pinMode(12,OUTPUT);
pinMode(A0,INPUT);
// set all the LOWother pins you’re using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
}

void loop() {
// if the switch is high, motor will turn on one direction:
if (analogRead(A0)>0)
{
digitalWrite(motor2Pin, LOW);
digitalWrite(motor1Pin, LOW);
} // set leg 1 of the H-bridge low
else
{
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
}
}

This was my first project and there can be some mistakes. If you are looking for starting electronic projects, Arduino is a not bad choice.

Thank you for reading

--

--

Naga Surya Praveen E

Programmer | Math lover | Positive thinker | I am not a robot 🤖