We'll look at how to connect an HC-SR04 range sensor to a Raspberry Pi Pico and use micro-ROS to post its values to the ROS 2 graph. This post expands on the previous one, ‘Getting started with micro-ROS on the Raspberry Pi Pico.' As a result, if you're unfamiliar with the subject, I recommend reading it first.
Sonar Sensors
We'll utilize a sonar, which is one of the most used sensors in robotics. Under ideal conditions, these sensors are inexpensive, simple to use, and remarkably accurate. Distances are measured with sonars. As a result, they may be used to identify and locate impediments, allowing a robot to avoid them. It's not difficult to see why they're so popular.
So, what exactly is a sonar, how does it operate, and which should I choose?
A sonar is an echolocation sensor that may be used to calculate distances. Sonars function in the same way as bats do to keep things simple to understand. They don't fly around at morning hunting bugs, nor do they combat crime. Obviously, I was referring to the way bats view the world. An transmitter and a receiver make up a sonar.
The former sends out an ultrasonic signal that bounces off of nearby objects and returns to the receiver. The distance travelled by the sound wave, and hence the distance to the obstruction that echoed the sound, may be simply calculated by monitoring the time difference between signal emission and reception.
If, for example, the sound wave is echoed away from the sensor, this technique might suffer from a variety of problems. Or if two barriers at different distances bounce it back to the sensor. Alternatively, it might be absorbed by a sound-dampening substance.
If you're still confused or want to learn more, I'll recommend you to your favorite online search engine because it's a bit outside the scope of this essay.
HC-SR04 Range Sensor
Connecting the HC-SR04 Range Sonar Sensor to the Raspberry Pi Pico
- 3V3 OUT (pin 36) to the ‘Vcc' of the sonar
- GND (pin 38) to the ‘Gnd' of the sonar
- GPIO 6 (pin 9) is connected to the ‘Echo' function of the sonar.
- GPIO 7 (pin 10) is connected to the sonar's 'Trig'.
Image Credit: Jeremie Deray |
Micro-ROS code for the Raspberry Pi Pico
// The GPIO pins to which the sonar is wired
#define GPIO_ECHO 6
#define GPIO_TRIGGER 7
/**
* @brief Get the range value in meter.
*/
float read_range() {
// Send an impulse trigger of 10us
gpio_put(GPIO_TRIGGER, 1);
sleep_us(10);
gpio_put(GPIO_TRIGGER, 0);
// Read how long is the echo
uint32_t signaloff, signalon;
do {
signaloff = time_us_32();
} while (gpio_get(GPIO_ECHO) == 0);
do {
signalon = time_us_32();
} while (gpio_get(GPIO_ECHO) == 1);
// Actual echo duration in us
const float dt = signalon - signaloff;
// distance in meter:
// echo duration (us) x speed of sound (m/us) / 2 (round trip)
return dt * 0.000343 / 2.0;
}
...
/**
* @brief Read the range from the sensor,
* fill up the ROS message and publish it.
*/
void timer_callback(rcl_timer_t *timer, int64_t /*last_call_time*/) {
if (timer) {
range_msg.range = read_range();
fill_msg_stamp(range_msg.header.stamp);
rcl_publish(&publisher, &range_msg, NULL);
} else {
printf("Failed to publish range. Continuing.\n");
}
}
docker run -it --rm -v /dev:/dev --privileged --net=host microros/micro-ros-agent:foxy serial --dev /dev/ttyACM0 -b 115200
$ ros2 topic list
/parameter_events
/pico/range
/rosout
$ ros2 topic echo /pico/range
header:
stamp:
sec: 145
nanosec: 837599000
frame_id: pico_sonar_0_link
radiation_type: 0
field_of_view: 30.0
min_range: 0.019999999552965164
max_range: 4.0
range: 12.138598442077637
---
header:
stamp:
sec: 145
nanosec: 915356000
frame_id: pico_sonar_0_link
radiation_type: 0
field_of_view: 30.0
min_range: 0.019999999552965164
max_range: 4.0
range: 12.138941764831543
---
Posts You May like:
- Raspberry Pi Pico: ADC Sampling and FFT
- Using CircuitPython for RP2040
- How to Setup Pico RP2040 on Windows
- Using micro-ROS on the Raspberry Pi Pico
- LED Tricks Using The Raspberry Pi Pico
- The RP2040 Raspberry Pi Pico Meets LoRa
- Pico supports SD cards and FatFS
- How to connect a Raspberry Pi Pico to LoRaWAN
- 50 Raspberry Pi Hacks & Tips You Should Know
- How to Install Wi-Fi and Internet on a Raspberry Pi Pico
- Easiest Way to to Run DC Motor with Raspberry Pi Pico
- Drag-and-Drop Programming For The Raspberry Pi Pico
- Make Pico DIY Project Without Any Soldering or Breadboard
- Detailed Comparison of Arduino Nano RP2040 Connect Vs Raspberry Pi Pico
- Make a USB Microphone Using the Raspberry Pi Pico
- The RP2040 is Now Available for $1
- Pico LoRa Expansion is Finally Out! Supports 868MHz
- CircuitPython 6.3.0 is Now Available with Improvement & Fixes
- Pico DIY Projects! Must Try 17 Different Pico HATs & Expansions