Introduction
Building a pre-programmed robot in a few lines of code is cool, but building an obstacle-avoidant, autonomous Raspberry Pi robot is even cooler!
In this guide, we'll show you how to incorporate a HC-SR04 Ultrasonic distance sensor to the Raspberry Pi robot. It will be programmed with Python using GPIO Zero.
In this guide, we'll show you how to incorporate a HC-SR04 Ultrasonic distance sensor to the Raspberry Pi robot. It will be programmed with Python using GPIO Zero.
After completing this guide, you will have a fully autonomous obstacle-avoidant two-wheeled robot.
Tools
-
-
-
-
-
-
-
-
-
-
-
-
from gpiozero import DistanceSensor from time import sleep sensor = DistanceSensor(echo=8, trigger=24) while True: print('Distance: ', sensor.distance * 100) sleep(1)
-
from gpiozero import DistanceSensor from gpiozero import Robot from time import sleep robot = Robot(left = (27, 17), right = (22, 23)) sensor = DistanceSensor(echo=8, trigger=24) while True: distance_to_object = sensor.distance * 100 print distance_to_object if distance_to_object <= 25: robot.backward() sleep(1) robot.left() sleep(1) else: robot.forward() sleep(0.1)
-