Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Robotics is an exciting field that combines various disciplines, including electronics, mechanics, and programming, to create machines that can perform tasks autonomously or semi-autonomously. Arduino, an open-source electronics platform, is a popular choice for hobbyists and engineers alike to prototype and develop robotic projects due to its simplicity and versatility. This article will guide you through creating a simple robotic project using Arduino, providing practical examples and sample codes.
Examples:
A line-following robot is a classic beginner robotics project. It uses sensors to detect and follow a line drawn on the floor.
Components Needed:
Sample Code:
#define LEFT_SENSOR_PIN A0
#define RIGHT_SENSOR_PIN A1
#define MOTOR_LEFT_FORWARD 5
#define MOTOR_LEFT_BACKWARD 6
#define MOTOR_RIGHT_FORWARD 9
#define MOTOR_RIGHT_BACKWARD 10
void setup() {
pinMode(LEFT_SENSOR_PIN, INPUT);
pinMode(RIGHT_SENSOR_PIN, INPUT);
pinMode(MOTOR_LEFT_FORWARD, OUTPUT);
pinMode(MOTOR_LEFT_BACKWARD, OUTPUT);
pinMode(MOTOR_RIGHT_FORWARD, OUTPUT);
pinMode(MOTOR_RIGHT_BACKWARD, OUTPUT);
}
void loop() {
int leftSensorValue = analogRead(LEFT_SENSOR_PIN);
int rightSensorValue = analogRead(RIGHT_SENSOR_PIN);
if (leftSensorValue < 500 && rightSensorValue < 500) {
// Move forward
digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
digitalWrite(MOTOR_RIGHT_FORWARD, HIGH);
digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
} else if (leftSensorValue > 500) {
// Turn right
digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
digitalWrite(MOTOR_RIGHT_FORWARD, LOW);
digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
} else if (rightSensorValue > 500) {
// Turn left
digitalWrite(MOTOR_LEFT_FORWARD, LOW);
digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
digitalWrite(MOTOR_RIGHT_FORWARD, HIGH);
digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
}
}
This code reads the values from the line tracking sensors and controls the motors to follow a line. The robot moves forward when both sensors detect the line, turns right when the left sensor detects the line, and turns left when the right sensor detects the line.
An obstacle-avoiding robot can navigate around obstacles using ultrasonic distance sensors.
Components Needed:
Sample Code:
#define TRIG_PIN 12
#define ECHO_PIN 11
#define MOTOR_LEFT_FORWARD 5
#define MOTOR_LEFT_BACKWARD 6
#define MOTOR_RIGHT_FORWARD 9
#define MOTOR_RIGHT_BACKWARD 10
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(MOTOR_LEFT_FORWARD, OUTPUT);
pinMode(MOTOR_LEFT_BACKWARD, OUTPUT);
pinMode(MOTOR_RIGHT_FORWARD, OUTPUT);
pinMode(MOTOR_RIGHT_BACKWARD, OUTPUT);
}
long readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
long distance = readDistance();
if (distance < 20) {
// Obstacle detected, turn right
digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
digitalWrite(MOTOR_RIGHT_FORWARD, LOW);
digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
delay(500);
} else {
// Move forward
digitalWrite(MOTOR_LEFT_FORWARD, HIGH);
digitalWrite(MOTOR_LEFT_BACKWARD, LOW);
digitalWrite(MOTOR_RIGHT_FORWARD, HIGH);
digitalWrite(MOTOR_RIGHT_BACKWARD, LOW);
}
}
This code uses an ultrasonic sensor to measure the distance to the nearest obstacle. If the robot detects an obstacle within 20 cm, it turns right; otherwise, it moves forward.