Arduino UNO-Based Ultrasonic Distance Sensor with ROS Integration
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit involves two Arduino UNO microcontrollers and an Ultrasonic Sensor. The first Arduino UNO is responsible for receiving and processing distance data from the Ultrasonic Sensor, while the second Arduino UNO subscribes to the sensor data and prints it to the Serial Monitor. The circuit is designed to measure distances using the Ultrasonic Sensor and communicate the data between the two microcontrollers using ROS (Robot Operating System).
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: +VCC, Trigger, Echo, GND
Wiring Details
Arduino UNO (Microcontroller 1)
- 5V: Connected to +VCC of the Ultrasonic Sensor
- GND: Connected to GND of the Ultrasonic Sensor
- GND: Connected to GND of the second Arduino UNO
- D9: Connected to Echo of the Ultrasonic Sensor
- D8: Connected to Trigger of the Ultrasonic Sensor
- D1: Connected to D0 of the second Arduino UNO
- D0: Connected to D1 of the second Arduino UNO
Arduino UNO (Microcontroller 2)
- GND: Connected to GND of the first Arduino UNO
- D1: Connected to D0 of the first Arduino UNO
- D0: Connected to D1 of the first Arduino UNO
Ultrasonic Sensor
- +VCC: Connected to 5V of the first Arduino UNO
- GND: Connected to GND of the first Arduino UNO
- Echo: Connected to D9 of the first Arduino UNO
- Trigger: Connected to D8 of the first Arduino UNO
Code Documentation
Arduino UNO (Microcontroller 1)
#include <ros.h>
#include <std_msgs/Float32.h>
// Ultrasonic Sensor Pins
const int trigPin = 8;
const int echoPin = 9;
// ROS Node Handle
ros::NodeHandle nh;
// Message to publish sensor data
std_msgs::Float32 distance_msg;
ros::Publisher distance_pub("sensor_data", &distance_msg);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
nh.initNode();
nh.advertise(distance_pub);
}
void loop() {
long duration;
float distance;
// Trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the distance
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2; // Convert to cm
// Publish distance to ROS
distance_msg.data = distance;
distance_pub.publish(&distance_msg);
nh.spinOnce();
delay(100);
}
Arduino UNO (Microcontroller 2)
#include <ros.h>
#include <std_msgs/Float32.h>
// ROS Node Handle
ros::NodeHandle nh;
// Callback function to handle incoming sensor data
void sensorDataCallback(const std_msgs::Float32& msg) {
Serial.print("Received Distance: ");
Serial.println(msg.data); // Print the received data
}
// Subscriber for the "sensor_data" topic
ros::Subscriber<std_msgs::Float32> sensor_data_sub("sensor_data", &sensorDataCallback);
void setup() {
Serial.begin(9600); // For debugging via Serial Monitor
nh.initNode();
nh.subscribe(sensor_data_sub);
}
void loop() {
nh.spinOnce(); // Process ROS messages
delay(100);
}