Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Raspberry Pi 4B Battery-Powered Object Detection with HC-SR04 Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Raspberry Pi 4B Battery-Powered Object Detection with HC-SR04 Ultrasonic Sensor

Circuit Documentation

Summary

This circuit is designed to interface a Raspberry Pi 4B with an HC-SR04 Ultrasonic Sensor for object detection. The Raspberry Pi is powered by a 9V battery and communicates with the ultrasonic sensor to measure distances. The sensor's TRIG and ECHO pins are connected to the GPIO17 and GPIO18 pins of the Raspberry Pi, respectively. The sensor's VCC and GND pins are connected to the 5V and GND pins of the Raspberry Pi.

Component List

  1. Raspberry Pi 4B

    • Description: A powerful single-board computer used for various applications including IoT, robotics, and more.
    • Pins: 3V3, 5V, GPIO2, GPIO3, GND, GPIO4, GPIO14, GPIO15, GPIO17, GPIO18, GPIO27, GPIO22, GPIO23, GPIO24, GPIO10, GPIO9, GPIO25, GPIO11, GPIO8, GPIO7, ID_SD, GPIO5, GPIO6, GPIO12, GPIO13, GPIO19, GPIO16, GPIO26, GPIO20, GPIO21, ID_SC, TR01_TAP, TR00_TAP, TR03_TAP, TR02_TAP, RUN, GLOBAL_EN
  2. 9V Battery

    • Description: A standard 9V battery used to power the Raspberry Pi.
    • Pins: +, -
  3. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic sensor used for measuring distances by emitting and receiving sound waves.
    • Pins: VCC, TRIG, ECHO, GND
  4. USB Plug

    • Description: A USB plug used for connecting to external devices.
    • Pins: GND, D+, D-, 5V+, Out

Wiring Details

Raspberry Pi 4B

9V Battery

HC-SR04 Ultrasonic Sensor

Code Documentation

Code Overview

The following Arduino sketch is designed to run on the Raspberry Pi 4B. It interfaces with the HC-SR04 Ultrasonic Sensor to measure distances. The sensor's TRIG pin is connected to GPIO17 and the ECHO pin to GPIO18. The sensor's VCC and GND are connected to the 5V and GND pins of the Raspberry Pi.

Code

#include <107-Arduino-AS504x.h>
#include <107-Arduino-APDS-9950.h>

/*
 * Arduino Sketch for Raspberry Pi 4B
 * Project: Object Detection with HC-SR04
 * This code interfaces with an HC-SR04 Ultrasonic Sensor to detect objects.
 * The sensor's TRIG pin is connected to GPIO17 and the ECHO pin to GPIO18.
 * The sensor's VCC and GND are connected to the 5V and GND pins of the Pi.
 */

const int trigPin = 17; // GPIO17 connected to TRIG pin of HC-SR04
const int echoPin = 18; // GPIO18 connected to ECHO pin of HC-SR04

void setup() {
  // Initialize serial communication at 9600 baud rate
  Serial.begin(9600);
  // Set trigPin as OUTPUT and echoPin as INPUT
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Set the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the echoPin, returns the sound wave travel time in microseconds
  long duration = pulseIn(echoPin, HIGH);
  // Calculate the distance
  float distance = duration * 0.034 / 2;
  // Print the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  // Wait for 500 milliseconds before the next loop
  delay(500);
}

Code Explanation