Controlling DC Motor with Relays

Hi all,

I'm trying to build a solar tracker for a project, so I need my motor to rotate in angles very slowly following the sun. As of now, I used an H-bridge to control the direction of the motor and it successfully follows the LDRs as inputs. The program compares the voltages of both LDRs and the motor moves to the direction where there is more light according to the LDRs. However, my motor is a DC geared motor connected to a shaft and I would like to control the direction with relays along with the H-bridge or completely replace the H-bridge with the relays, still using the LDRs as inputs. The issue is I don't really know how to do this since I'm not familiar with relays. Please help if you can. Attached below is the code.

#define E1 10  // Enable Pin for motor 1 
#define I1 8  // Control pin 1 for motor 1
#define I2 9  // Control pin 2 for motor 1

float tolerance = 0.25;
float diff = 0;

void setup() {

  //set the pin mode for the motor to be an output

   pinMode(E1, OUTPUT);
   pinMode(I1, OUTPUT);
   pinMode(I2, OUTPUT);
    
   Serial.begin(9600);
}

 

void loop() {

// read the value from the sensor

 float sensorValue = analogRead(A0);
 float sensorValue1 = analogRead(A1);

// Convert the analog value into voltage 

 float volt1=sensorValue*(5.0/1023.0);
 float volt2=sensorValue1*(5.0/1023.0);

 diff = volt1-volt2;
 
  if(-1*tolerance < diff && diff > tolerance)
  {
  if (volt1 > volt2)
  {
    digitalWrite(E1, 20);
    digitalWrite(I1, LOW);
    digitalWrite(I2, HIGH);  
  }
  }
 else if(-1*tolerance > diff && diff < tolerance){
  
    digitalWrite(E1, 20);
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
 }
 else{
  digitalWrite(E1, 0);  
 }
  
  //print voltages

  Serial.print("sensorValue: ");
  Serial.println(volt1);
  Serial.print(' ');
  Serial.print("sensorValue1: ");
  Serial.println(volt2);
  Serial.print("diffValue: ");
  Serial.println(diff);
  Serial.print('\n');

  delay (1000);

 /* //Motor speed changes relative to the value read at A0.

    analogWrite(E1, 153); // Run in half speed
  
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
 
    delay(10000);
 
    // change direction
 
    digitalWrite(E1, LOW);
 
    delay(200);
 
    analogWrite(E1, 255);  // Run in full speed
 
    digitalWrite(I1, LOW);
    digitalWrite(I2, HIGH);
 
    delay(10000);*/

}

Sorry, but your question makes no sense at all. You state you are using an H-bridge to control the direction the motor turns, but want to use a relay to control the direction the motor turns. Please clarify!

Paul

Hi Paul,

Thank you for replying. Sorry I wasn't clear. As of now, my project is using H-bridge, but I would like to use relays instead or use them along with the H-bridge (if possible). I'm not very familiar with relays, so please help if you can.

glopez98:
Hi Paul,

Thank you for replying. Sorry I wasn't clear. As of now, my project is using H-bridge, but I would like to use relays instead or use them along with the H-bridge (if possible). I'm not very familiar with relays, so please help if you can.

Please explain what you want a relay to do that the H-bridge is not doing! That is what you are not explaining.

Paul

I want the relay to allow the DC motor to rotate very slowly. The ideal output would be for the motor to move in angles but this is not really possible or is extremely difficult, according to what I have read. Therefore, I would like for the motor to rotate really slowly and follow the LDR that reads a higher voltage (follow the light/sun). With H-bridge, the motor rotates really quickly and I'm not able to control the speed at a degree a relay would. Also, I would like to control the amount of current going into the motor (which relay can do), since my motor has a high voltage and is going to be connected to the main distribution panel so I don't want it to drive that much current and voltage.

glopez98:
I want the relay to allow the DC motor to rotate very slowly. The ideal output would be for the motor to move in angles but this is not really possible or is extremely difficult, according to what I have read. Therefore, I would like for the motor to rotate really slowly and follow the LDR that reads a higher voltage (follow the light/sun). With H-bridge, the motor rotates really quickly and I'm not able to control the speed at a degree a relay would. Also, I would like to control the amount of current going into the motor (which relay can do), since my motor has a high voltage and is going to be connected to the main distribution panel so I don't want it to drive that much current and voltage.

Do you see these lines in your program:

analogWrite(E1, 153); // Run in half speed

and

analogWrite(E1, 255); // Run in full speed ?

if you wrote: analogWrite(E1, 0);

Your motor will stop.

You can vary the speed because the code is creating a pulsed power to the motor.

A relay is a switch. Only fully on or fully off. Anything in between is not possible.

Concentrate on your existing program and find where to adjust the pulse rate for your H-bridge.

Paul