2. Wiring A Robot - FAAMT/ros_ugv_ultron GitHub Wiki

Connection Guide:
- 5V pin on Arduino Mega 2560 to 5V pin on both motor drivers.
- GND pin on Arduino Mega 2560 to GND pin on both motor drivers.
- Pin 2 to DC1IN1, Pin 3 to DC1IN2, Pin 4 to DC1ENA.
- Pin 5 to DC1IN3, Pin 6 to DC1IN4, Pin 7 to DC1ENB.
- Pin 8 to DC2IN1, Pin 9 to DC2IN2, Pin 10 to DC2ENA.
- Pin 11 to DC2IN3, Pin 12 to DC2IN4, Pin 13 to DC2ENB.
These connections allow you to control the motors effectively using the Arduino Mega 2560 and L298N motor driver. We'll demonstrate this with a working code example and a short demo video showcasing the motor functionality.
Next, we'll connect the Arduino Mega to the Raspberry Pi and utilize two ROS packages to develop the teleoperation feature: exmachina (dev) & rosserial (git). This combination enables us to send keyboard input to the robot, which the microcontroller can interpret in various ways. In this case, we use it to drive the motors.
Arduino Mega 2560 PINS | L298N Driver DC1/DC2 PINS | Color Scheme 🔌 |
---|---|---|
5V | 5V (both drivers) | ![]() |
GND | GND (both drivers) | ![]() |
2 | DC1IN1 | ![]() |
3 | DC1IN2 | ![]() |
4 | DC1ENA | ![]() |
5 | DC1IN3 | ![]() |
6 | DC1IN4 | ![]() |
7 | DC1ENB | ![]() |
8 | DC2IN1 | ![]() |
9 | DC2IN2 | ![]() |
10 | DC2ENA | ![]() |
11 | DC2IN3 | ![]() |
12 | DC2IN4 | ![]() |
13 | DC2ENB | ![]() |
The relevant source file for this phase of the project is the arduino_ws/ultron_move.ino (C++).
Motor Pin Definitions:
In this segment, the code defines constants for the pins used to control the motors of a robot. Each motor requires four input pins (IN1, IN2, IN3, IN4) to control the direction and two enable pins (ENA, ENB). The constants are given meaningful names like DC1IN1, DC1IN2, etc., to represent the specific pins connected to Motor 1, and similar constants for Motor 2.
// Definitions for Motor Pins
const int DC1IN1 = 2; // Motor 1, Input 1
const int DC1IN2 = 3; // Motor 1, Input 2
const int DC1IN3 = 5; // Motor 1, Input 3
const int DC1IN4 = 6; // Motor 1, Input 4
const int DC1ENA = 4; // Motor 1, Enable A
const int DC1ENB = 7; // Motor 1, Enable B
const int DC2IN1 = 8; // Motor 2, Input 1
const int DC2IN2 = 9; // Motor 2, Input 2
const int DC2IN3 = 11; // Motor 2, Input 3
const int DC2IN4 = 12; // Motor 2, Input 4
const int DC2ENA = 10; // Motor 2, Enable A
const int DC2ENB = 13; // Motor 2, Enable B
Setup Function:
The setup function is a special function in Arduino that runs once when the program starts. In this segment, the code begins serial communication at a baud rate of 9600, which allows communication with a computer for debugging or sending data. It then sets all the motor pins as outputs using the pinMode function. This step is necessary to ensure that the pins are configured correctly for motor control.
void setup() {
Serial.begin(9600);
// Set all motor pins as outputs
pinMode(DC1IN1, OUTPUT);
pinMode(DC1IN2, OUTPUT);
pinMode(DC1IN3, OUTPUT);
pinMode(DC1IN4, OUTPUT);
pinMode(DC1ENA, OUTPUT);
pinMode(DC1ENB, OUTPUT);
pinMode(DC2IN1, OUTPUT);
pinMode(DC2IN2, OUTPUT);
pinMode(DC2IN3, OUTPUT);
pinMode(DC2IN4, OUTPUT);
pinMode(DC2ENA, OUTPUT);
pinMode(DC2ENB, OUTPUT);
}
Loop Function:
The loop function in Arduino repeatedly runs after the setup function. In this code segment, a test routine for the robot "ULTRON" is defined. It involves a sequence of movements with pauses in between. The wait function is used to stop the motors, and then a 3-second delay is introduced using delay(3000) to pause the robot.
Next, the forward, backward, left, and right functions are called one by one to make "ULTRON" move forward, backward, left, and right, respectively. Following each movement, the wait function is called again to stop the robot, and a 3-second delay is added to pause before the next movement.
Overall, the loop function controls the execution of the test routine, allowing "ULTRON" to perform a series of movements and pauses continuously
void loop() {
// Test Routine
wait(); // Stop the motors
delay(3000); // Delay 3 seconds
forward(); // Move ULTRON forward
delay(3000); // Delay 3 seconds
wait(); // Stop the motors
delay(3000); // Delay 3 seconds
backward(); // Move ULTRON backward
delay(3000); // Delay 3 seconds
wait(); // Stop the motors
delay(3000); // Delay 3 seconds
left(); // Turn ULTRON left
delay(3000); // Delay 3 seconds
wait(); // Stop the motors
delay(3000); // Delay 3 seconds
right(); // Turn ULTRON right
delay(3000); // Delay 3 seconds
wait(); // Stop the motors
delay(3000); // Delay 3 seconds
}
Motor Control Functions:
This segment contains four functions: forward, backward, left, and right, responsible for controlling specific movements of "ULTRON." These functions use the analogWrite function to set the motor speed by providing PWM signals to the enable pins (ENA and ENB) with a value of 200.
In each motor control function, specific input combinations are set on the input pins (IN1, IN2, IN3, IN4) to determine the direction of rotation for each wheel, enabling "ULTRON" to move forward, backward, turn left, or turn right.
The wait function is utilized to stop the robot by setting the speed to zero and ensuring all input pins are set to LOW, effectively halting all motors.
// Function to move ULTRON forward
void forward(void) {
analogWrite(DC1ENA, 200);
analogWrite(DC1ENB, 200);
analogWrite(DC2ENA, 200);
analogWrite(DC2ENB, 200);
// Set appropriate input combinations for each wheel to move forward
digitalWrite(DC1IN1, HIGH);
digitalWrite(DC1IN2, LOW);
digitalWrite(DC1IN3, LOW);
digitalWrite(DC1IN4, HIGH);
digitalWrite(DC2IN1, HIGH);
digitalWrite(DC2IN2, LOW);
digitalWrite(DC2IN3, LOW);
digitalWrite(DC2IN4, HIGH);
}
// Function to move ULTRON backward
void backward(void) {
analogWrite(DC1ENA, 200);
analogWrite(DC1ENB, 200);
analogWrite(DC2ENA, 200);
analogWrite(DC2ENB, 200);
// Set appropriate input combinations for each wheel to move backward
digitalWrite(DC1IN1, LOW);
digitalWrite(DC1IN2, HIGH);
digitalWrite(DC1IN3, HIGH);
digitalWrite(DC1IN4, LOW);
digitalWrite(DC2IN1, LOW);
digitalWrite(DC2IN2, HIGH);
digitalWrite(DC2IN3, HIGH);
digitalWrite(DC2IN4, LOW);
}
// Function to turn ULTRON left
void left(void) {
analogWrite(DC1ENA, 200);
analogWrite(DC1ENB, 200);
analogWrite(DC2ENA, 200);
analogWrite(DC2ENB, 200);
// Set appropriate input combinations for each wheel to turn left
digitalWrite(DC1IN1, HIGH);
digitalWrite(DC1IN2, LOW);
digitalWrite(DC1IN3, HIGH);
digitalWrite(DC1IN4, LOW);
digitalWrite(DC2IN1, LOW);
digitalWrite(DC2IN2, HIGH);
digitalWrite(DC2IN3, LOW);
digitalWrite(DC2IN4, HIGH);
}
// Function to turn ULTRON right
void right(void) {
analogWrite(DC1ENA, 200);
analogWrite(DC1ENB, 200);
analogWrite(DC2ENA, 200);
analogWrite(DC2ENB, 200);
// Set appropriate input combinations for each wheel to turn right
digitalWrite(DC1IN1, LOW);
digitalWrite(DC1IN2, HIGH);
digitalWrite(DC1IN3, LOW);
digitalWrite(DC1IN4, HIGH);
digitalWrite(DC2IN1, HIGH);
digitalWrite(DC2IN2, LOW);
digitalWrite(DC2IN3, HIGH);
digitalWrite(DC2IN4, LOW);
}
// Function to stop ULTRON
void wait(void) {
analogWrite(DC1ENA, 0);
analogWrite(DC1ENB, 0);
analogWrite(DC2ENA, 0);
analogWrite(DC2ENB, 0);
// Set all input combinations for each wheel to stop
digitalWrite(DC1IN1, LOW);
digitalWrite(DC1IN2, LOW);
digitalWrite(DC1IN3, LOW);
digitalWrite(DC1IN4, LOW);
digitalWrite(DC2IN1, LOW);
digitalWrite(DC2IN2, LOW);
digitalWrite(DC2IN3, LOW);
digitalWrite(DC2IN4, LOW);
}
a353832b-1e80-4e43-bec2-c3b36aa8ee4a.mp4
In this project, we establish a simple connection between the main computer and the microcontroller using a standard USB-A to USB-B wire. This connection facilitates seamless communication between the devices through the widely-used serial communication protocols provided by the rosserial (git) package. To aid in documentation, a diagram has been included to represent the setup visually.

The instructions on developing teleoperation for your robot can be referred to in order to replicate the demo.
In this project, the OV5647 camera module is connected to the Raspberry Pi's CSI port, utilizing the raspicam package within the ROS (Robot Operating System) environment. To view the camera's visual output in real-time, we utilize the rqt_image_view ROS node. The camera publishes its images on the "/raspicam_node/image/compressed" topic. By running the rqt_image_view node and subscribing to this topic, we can observe the live visual output from the OV5647 camera in the GUI window. This real-time image stream enhances the project's capabilities, enabling tasks like object detection, image processing, and computer vision-based applications.

The instructions on developing vision for your robot can be referred to in order to replicate the demo.
In this project, the YDLiDAR X2L LiDAR sensor is connected to the Raspberry Pi using the "ydlidar" ROS package. The package serves as the interface driver, facilitating communication with the sensor. LiDAR data is visualized in real-time through rviz, offering a 3D view of the surroundings. The continuous scanning of the YDLiDAR X2L enables SLAM, obstacle detection, and navigation.

The instructions on developing lidar for your robot can be referred to in order to replicate the demo.