In: Electrical Engineering
Write a program By using ardino and h-bridg and dc motor
No sensoer.
VICHAEL start from A move some distance till point B
than turn 90 degree than move some distance till C
than turn 90 degree and move Tell point D
than turn 90 degree and move till start point which is A
As square moving (, starting from A and back to A)
Let's consider the H bridge has an enable pin like most standard H bridges and two control pins as shown in the block diagram below. ( I'll be considering the standard L293D H bridge for example code)
We need two such H bridges to control the two motors ( left and right ) individually. I assume that Arduino Uno is used but the code will be compatible with any Arduino. The code is commented describing itself clearly. Screenshots of the code have been attached as well for easy readability.
#define left_dir1_pin 12 // Defining all the pins for different signals
#define left_dir0_pin 11 // If some of these pins are already used they can
#define right_dir1_pin 10 // be changed here to any other GPIO pin.
#define right_dir0_pin 9
#define left_enable_pin 8
#define right_enable_pin 7
#define forward_delay 2000 // This delay decides how far the bot
// moves in straight lines
#define turn_delay 1500 // This defines how much the bot turns
// This delay will have to be adjusted
// based on the motor and power supply
// to achieve 90 degree turns.
void setup() {
pinMode(left_dir1_pin,OUTPUT); // Here we set all the control signals to the
pinMode(left_dir0_pin,OUTPUT); // H-bridge as outputs so that we can write
pinMode(right_dir1_pin,OUTPUT); // high and low signals to the inputs of the
pinMode(right_dir0_pin,OUTPUT); // H-bridge
pinMode(left_enable_pin,OUTPUT);
pinMode(right_enable_pin,OUTPUT);
}
void forward()
{
digitalWrite(left_dir1_pin,HIGH); // Set left dir1 pin high
digitalWrite(left_dir0_pin,LOW); // Set left dir0 pin low
digitalWrite(right_dir1_pin,HIGH); // Set right dir1 pin high
digitalWrite(right_dir0_pin,LOW); // Set right dir0 pin low
digitalWrite(left_enable_pin,HIGH); // Care must be taken if enable is active high or
digitalWrite(right_enable_pin,HIGH); // active low. Here we enable the outputs
}
void left()
{
digitalWrite(left_dir1_pin,LOW); // Set left dir1 pin low
digitalWrite(left_dir0_pin,HIGH); // Set left dir0 pin high
digitalWrite(right_dir1_pin,HIGH); // Set right dir1 pin high
digitalWrite(right_dir0_pin,LOW); // Set right dir0 pin low
digitalWrite(left_enable_pin,HIGH); // Care must be taken if enable is active high or
digitalWrite(right_enable_pin,HIGH); // active low. Here we enable the outputs
}
void right()
{
digitalWrite(left_dir1_pin,HIGH); // Set left dir1 pin high
digitalWrite(left_dir0_pin,LOW); // Set left dir0 pin low
digitalWrite(right_dir1_pin,LOW); // Set right dir1 pin low
digitalWrite(right_dir0_pin,HIGH); // Set right dir0 pin high
digitalWrite(left_enable_pin,HIGH); // Care must be taken if enable is active high or
digitalWrite(right_enable_pin,HIGH); // active low. Here we enable the outputs
}
void back()
{
digitalWrite(left_dir1_pin,LOW); // Set left dir1 pin low
digitalWrite(left_dir0_pin,HIGH); // Set left dir0 pin high
digitalWrite(right_dir1_pin,LOW); // Set right dir1 pin low
digitalWrite(right_dir0_pin,HIGH); // Set right dir0 pin high
digitalWrite(left_enable_pin,HIGH); // Care must be taken if enable is active high or
digitalWrite(right_enable_pin,HIGH); // active low. Here we enable the outputs
}
void halt()
{
digitalWrite(left_enable_pin,LOW); // Care must be taken if enable is active high or
digitalWrite(right_enable_pin,LOW); // active low. Here we disable the outputs
}
void loop() {
forward(); // Move forward from A
delay(forward_delay); // Delay to reach point B
left(); // Turn left (90 degrees)
delay(turn_delay);
forward(); // Move from point B
delay(forward_delay); // Delay to reach point C
left(); // Turn left (90 degrees)
delay(turn_delay);
forward(); // Move from point C
delay(forward_delay); // Delay to reach point D
left(); // Turn left (90 degrees)
delay(turn_delay);
forward(); // Move from point D
delay(forward_delay); // Delay to reach point A again
halt(); // Stop the bot
while(1); // Infinite loop
}