DC Motor Control via Infrared Sensor & Remote

This project accomplishes a few objectives. First, the use of a KY-022 Infrared sensor is incorporated for general familiarity. Second, the IR remote control emits HEX codes for each button pressed, so it is necessary to capture those HEX codes to map them to specific button IDs and functions later developed. Third, to integrate the motor control driver (L293D) with the Arduino Nano to control motor activity such as speed and direction.

The project is supplied by two separate power supplies. The Arduino Nano and IR sensor is powered by the USB connection while the motor driver is powered by the regulated +5VDC supply. The DC motor operates in both forward and reverse motion depending upon a positive or negative voltage presented by the driver. The higher the positive or negative voltage, the higher the speed. The lower the voltage presented from the driver, the lower the speed. The propeller mounted to the DC motor shaft is simply for ease of visibility.

The L293D motor driver interfaces with the Arduino Nano at a few pins. Specifically, forward and backward motor shaft direction (pin 2 & 7) and speed (pin 1). Code control of these GPIO pins is set up through digitalWrite operation for high/low binary direction assignment while the speed of the motor is set by an analogWrite instruction (from 0 to 255). The IR remote control provides for power on / off, forward or reverse direction, increase speed, and decrease speed. The capacitors bypassed at the motor connections are to filter noise that may appear from unwanted radiation conducted back into the system. These capacitors help to snub back EMF otherwise present throughout the circuit.



The Arduino Nano is programmed through the same USB port that powers it. The USB power and data connection to a local computer is not illustrated in this schematic as it is assumed. After programming the USB is detachable provided the Nano is powered by an alternate source such as a power bank, battery, or wall adapter. The connections throughout this system are illustrated for pin-to-pin connectivity between the motor driver, the Arduino Nano, the IR Sensor, and the DC motor.

Arduino Setup:

See the full code in text format below for copy and paste use.
Serial Monitor: During set up, it was necessary to set up a temporary remote control read operation through the IR sensor to get all of the HEX addresses emitted for each keypress. All button names were pressed to get their corresponding HEX values whereby the mapping is complete to assign button unique functions through conditional statements within the program.
Serial Monitor: To track actual IR sensor readings that correspond to motor activity, the Serial Monitor reports back what remote control button press activity occurs. In this example view, the down button is pressed repeatedly to slow motor speed, and the up button is pressed repeatedly to increase motor speed. The power on (pwr) and power off (func) events record the on/off activity of the motor through the IR remote control and its corresponding sensor.

Code Setup:


#include <IRremote.h>

int IRpin=9;
IRrecv IR(IRpin);
decode_results cmd;
String myCom;

int speedPin=5;
int dir1=4;
int dir2=3;
int mSpeed=205;

void setup() {
Serial.begin(9600);
IR.enableIRIn();

pinMode(speedPin,OUTPUT);
pinMode(dir1,OUTPUT);
pinMode(dir2,OUTPUT);
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
}

void loop() {
while (IR.decode(&cmd)==0){
}

//Serial.println (cmd.value,HEX);
delay(1000);
IR.resume();

if (cmd.value==0xFF6897) {
myCom=”zero”;
Serial.println(myCom);
}
if (cmd.value==0xFF30CF) {
myCom=”one”;
Serial.println(myCom);
}
if (cmd.value==0xFF18E7) {
myCom=”two”;
Serial.println(myCom);
}
if (cmd.value==0xFF7A85) {
myCom=”three”;
Serial.println(myCom);
}
if (cmd.value==0xFF10EF) {
myCom=”four”;
Serial.println(myCom);
}
if (cmd.value==0xFF38C7) {
myCom=”five”;
Serial.println(myCom);
}
if (cmd.value==0xFF5AA5) {
myCom=”six”;
Serial.println(myCom);
}
if (cmd.value==0xFF42BD) {
myCom=”seven”;
Serial.println(myCom);
}
if (cmd.value==0xFF4AB5) {
myCom=”eight”;
Serial.println(myCom);
}
if (cmd.value==0xFF52AD) {
myCom=”nine”;
Serial.println(myCom);
}
if (cmd.value==0xFFA25D) {
myCom=”pwr”;
Serial.println(myCom);
}
if (cmd.value==0xFF629D) {
myCom=”v+”;
Serial.println(myCom);
}
if (cmd.value==0xFFA857) {
myCom=”v-“;
Serial.println(myCom);
}
if (cmd.value==0xFFE21D) {
myCom=”func”;
Serial.println(myCom);
}
if (cmd.value==0xFF22DD) {
myCom=”rwd”;
Serial.println(myCom);
}
if (cmd.value==0xFF02FD) {
myCom=”play”;
Serial.println(myCom);
}
if (cmd.value==0xFFC23D) {
myCom=”ff”;
Serial.println(myCom);
}
if (cmd.value==0xFFE01F) {
myCom=”down”;
Serial.println(myCom);
}
if (cmd.value==0xFF906F) {
myCom=”up”;
Serial.println(myCom);
}
if (cmd.value==0xFF9867) {
myCom=”eq”;
Serial.println(myCom);
}
if (cmd.value==0xFFB04F) {
myCom=”st”;
Serial.println(myCom);
}
if (myCom==”pwr”){
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
analogWrite(speedPin,205);
}
if (myCom==”func”){
digitalWrite(dir1,LOW);
digitalWrite(dir2,LOW);
analogWrite(speedPin,0);
}
if (myCom==”ff”){
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
analogWrite(speedPin,mSpeed);
}
if (myCom==”rwd”){
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
analogWrite(speedPin,mSpeed);
}
if (myCom==”up”){
mSpeed=mSpeed+5;
if (mSpeed>255){
mSpeed=255;
}
analogWrite(speedPin,mSpeed);
}
if (myCom==”down”){
mSpeed=mSpeed-5;
if (mSpeed<0){
mSpeed=0;
}
analogWrite(speedPin,mSpeed);
}
delay(500);
}


About

Servant of Christ Jesus. U.S. Military Veteran, Electrical Engineer, Pepperdine MBA, and M.A. Biblical and Theological Studies.

, ,

Comments are closed.