DC Run motor control with IR sensor

Hello everyone,

I am trying to control a DC motor with IR sensors. I know the motor works as I have ran the motor sucsessfuly without the IR sensor; however, when I try to run it with the sensors nothing happens. This is part of a bigger project but since I am having a difficult time I've scaled it back to just this basic concept so I can then build from there when I get this working. I've looked at other posts from people with similar issues and I feel like i've tried everything but nothing has worked. Appreciate any feedback I can get. Thanks!


//motor 2: controls bottom conveyor
int motor2pin1 = 7;
int motor2pin2 = 8;
int en2 = 9;

//sensors
int sensor1 = A1;
int sensor2 = A2;
void setup() {
  
  pinMode(en2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);

  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);

  analogWrite(motor2pin1, LOW);
  analogWrite(motor2pin2, LOW);

}

void loop() {
  // put your main code here, to run repeatedly:

  int irvalue1 = analogRead(sensor1);
  int irvalue2 = analogRead(sensor2);

  if (irvalue1 == HIGH)
  {

    digitalWrite(motor2pin1, HIGH);
    digitalWrite(motor2pin1, LOW);
  }

  if (irvalue2 == HIGH)
  {

        digitalWrite(motor2pin1, LOW);
        digitalWrite(motor2pin1, LOW);
  }

  

  



}

These are fighting each other, which is it?

The arguments for analogWrite are incorrect. Not HIGH or LOW, should be a value from 0-255.
Did you mean digitalWrite? That's what you used later in the sketch for these motor pins.
https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
I'm not sure if the way you have LOW will still evaluate to 0 if that's what you meant although maybe it will? I have never tried.

This is incorrect. You're asking to evaluate whether something is HIGH/LOW when the sensor input is 0-1023. See the link I posted in post 3 and just try that in your sketch. If you are using an Uno, you'll have to change the motor pin assignments to one of the PWM pins (3,5,6,9,10 or 11). You currently use 7 and 8 and PWM isn't supported on those for the Uno. You didn't give other details about your motor controller either, so it's difficult to get an exact handle on your troubles.

You never enable the motor driver channel.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.