Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Help with the Arduino code. Controlling a motor using IR Sensor

Status
Not open for further replies.

rishiraj

Junior Member level 1
Joined
Dec 25, 2012
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,393
This is a very basic problem but am not able to find a solution.

I'm just trying to control a motor (ON/OFF) using IR Sensor. I wrote my program code in such a way that if the IR Ray gets reflected then the sensor detects a 'HIGH' signal and he motor should start rotating. But maybe due to my utter stupid programming, its not working. Please help to rectify the code. I googled around other sources to find that whenever IR Sensors are used, the serial communication port of Arduino is also used but I don't know what changes shall I make in my code to make use of them in a productive manner.

This is my code:

Code:
#define Motor11 5
#define Sensor 9 
int val=0;

void setup()
{
  pinMode(Motor11,OUTPUT);
  
  pinMode(Sensor,INPUT);
}
void loop()
{
 
  val=analogRead(Sensor);
  if(val==HIGH)
  {
     analogWrite(Motor11,HIGH);
     delay(1000);
     
  }
  else
  {
    analogWrite(Motor11,LOW);
    delay(1000);
  }
}

Also to mention, I m using a H-Bridge for the motor which is externally powered using a 9V battery, the IR Sensor is powered from the (Vin) pin of Arduino, the entire circut has a common ground.
 

Hello

I am also not well versed with Arduino but from my experience in using IR in other controllers i think that you should use just digital read to get the HIGH/LOW from an IR sensor...no need to link it to the ADC. Use the port as digital Input and use digitalRead(sensor) and also use DigitalWrite(Motor11,HIGH/LOW). Try this hope it works not sure.
 

Hi..
1.) since u are only interested in whether there is a high o/p from the sensor or not, there is no need to use the ADC. use it as a normal input pin.
2.) what sort of an o/p are u getting.....

cheers,
Vijay
 

Hello

I am also not well versed with Arduino but from my experience in using IR in other controllers i think that you should use just digital read to get the HIGH/LOW from an IR sensor...no need to link it to the ADC. Use the port as digital Input and use digitalRead(sensor) and also use DigitalWrite(Motor11,HIGH/LOW). Try this hope it works not sure.

Yeah, changed all inputs and outputs to digital but still its not working. Don't know whats wrong. When I connect a LED to detect weather the high signal from Sensor is working then I found that its working fine. I men, the only problem is that, the motor does not turn on. The motor and H-bridge is working fine, I checked it.

- - - Updated - - -

Hi..
1.) since u are only interested in whether there is a high o/p from the sensor or not, there is no need to use the ADC. use it as a normal input pin.
2.) what sort of an o/p are u getting.....

cheers,
Vijay

1. Did this way, but problem still persists. Changed all to digital
2. The sensors, Motor, H, bridge are all working fine, I checked it. The sensor can make an LED glow if it detects a HIGH signal (i.e. if the IR is reflected back) but can't run the motor. I'm using an H-Bridge also for the motor but don't know, its not turning up.
 

just noticed...
you are using analogwrite() to control the motor....
try using digitalWrite()....
 

Did you change all analog read and write to Digitalread() and Digitalwrite() ??

- - - Updated - - -

if you are using a DC motor you can use an IC L293D it is a motor driver IC which has a built in H bridge. If nothing works try replacing your H bridge with this one.
 

Did you change all analog read and write to Digitalread() and Digitalwrite() ??

- - - Updated - - -

if you are using a DC motor you can use an IC L293D it is a motor driver IC which has a built in H bridge. If nothing works try replacing your H bridge with this one.

Yes, changed all 'analog' to 'digital' but not working still. I'm using a H Bridge that has L293D in it, I mean its a L293D H-bridge.

I changed my code to this one below so that all pins remain defined, but still not working. :(

Code:
#define Motor11 5
#define Motor12 6
#define Motor21 10
#define Motor22 11
#define Sensor 9 

int val=0;

void setup()
{
  pinMode(Motor11,OUTPUT);
  pinMode(Motor12,OUTPUT);
  pinMode(Motor21,OUTPUT);
  pinMode(Motor22,OUTPUT);  
  pinMode(Sensor,INPUT);
}
void loop()
{
 
  val=digitalRead(Sensor);
  if(val==HIGH)
  {
     digitalWrite(Motor11,HIGH);
     delay(1000);
     
  }
  else
  {
    digitalWrite(Motor11,LOW);
    delay(1000);
    
  }
  

}
 
Last edited:

#define Motor11 5
#define Motor12 6
#define Motor21 10
#define Motor22 11
#define Sensor 9

int val=0;

void setup()
{
pinMode(Motor11,OUTPUT);
pinMode(Motor12,OUTPUT);
pinMode(Motor21,OUTPUT);
pinMode(Motor22,OUTPUT);
/////////pinMode(Sensor,INPUT); //not required to defined.by doing so u are using it as a digital input
}
void loop()
{

val=analogRead(Sensor);
if(val>0)
{
digitalWrite(Motor11,HIGH);
//delay(1000);

}
else
{
digitalWrite(Motor11,LOW);
//delay(1000);

}


}

if it still doesnt work, may be its a hardware issue...
1. try using a ULN driver to drive the motors.....
2. make sure there is a common ground between the power supplies.....
3. post the specs of the motor u are trying to control and a schematic of your hardware arrangement

- - - Updated - - -

#define Motor11 5
#define Motor12 6
#define Motor21 10
#define Motor22 11
#define Sensor 9

int val=0;

void setup()
{
pinMode(Motor11,OUTPUT);
pinMode(Motor12,OUTPUT);
pinMode(Motor21,OUTPUT);
pinMode(Motor22,OUTPUT);
/////////pinMode(Sensor,INPUT); //not required to defined.by doing so u are using it as a digital input
}
void loop()
{

val=analogRead(Sensor);
if(val>0)
{
digitalWrite(Motor11,HIGH);
//delay(1000);

}
else
{
digitalWrite(Motor11,LOW);
//delay(1000);

}


}

if it still doesnt work, may be its a hardware issue...
1. try using a ULN driver to drive the motors.....
2. make sure there is a common ground between the power supplies.....
3. post the specs of the motor u are trying to control and a schematic of your hardware arrangement
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top