Skip to content

Week 11. Input Devices

Group Assignment:

  • Probe an input device(s)’s analog levels and digital signals.
  • Document your work on the group work page and reflect on your individual page what you learned.

Group Assignment Link

Individual Assignment:

  • Add a sensor to a microcontroller board that you have designed and read it.

This week for Input Devices, we need to add an input device to the micro controller board that we have designed and produced during Electronics Design Week.

HC-SR04 Ultrasonic Distance Sensor

  • The HC-SR04 Ultrasonic Distance Sensor is a sensor used for detecting the distance to an object using sonar.

The HC-SR04 Ultrasonic Range Sensor Features:

  • Input Voltage: 5V
  • Current Draw: 20mA (Max)
  • Digital Output: 5V
  • Digital Output: 0V (Low)
  • Working Temperature: -15°C to 70°C
  • Sensing Angle: 30° Cone
  • Angle of Effect: 15° Cone
  • Ultrasonic Frequency: 40kHz
  • Range: 2cm - 400cm

HC-SR04 Dimensions: image from this source:

Setup for my ESP32 board:

  • Vcc - 5V
  • Gnd - Gnd
  • Echo - GPIO14
  • Trig - GPIO27

Code:

//code edited from chatGPT
#include <Wire.h>
#define echoPin 14             
#define trigPin 27             
long duration, distance;
void setup(){
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
void loop(){
  // trig send a signal and when signal refelcts back and received, distance calculated 
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58.2;
  String disp = String(distance);

  Serial.print("Distance: ");
  Serial.print(disp);
  Serial.println(" cm");
  delay(1000);
}

In this Arduino code I used, I set up the pins pin 14 for echo and pin 27 for trigger, which can be adjusted if needed. Upon initializing serial communication and configuring the pins in the setup function, I programmed the loop function to trigger the sensor by briefly setting the trigger pin high and low. Using pulseIn, I measured the time it took for the echo signal to return, which allowed me to calculate the distance in centimeters. This data was then transmitted over serial to the computer for display. This process occurred every second, providing continuous distance measurements in real time.

I also tried out the code with an XIAO ESP32 S3 board, and it worked perfectly with the ultrasonic sensors too. It’s cool to see how it can be used with different boards and still do its job well.

ESP32-S3 camera model

image from xiao source

  • I tested out the Xiao ESP32-S3 camera module using my custom-made ESP32 Xiao board.
  • I used ESP32 example code from the Arduino IDE.
  • First, I made sure to select the Xiao ESP32-S3 camera model in the code, and then I entered the correct Wi-Fi credentials.
  • Additionally, I went to the ‘Tools’ menu and selected ‘OPI PSRAM’ under ‘PSRAM’ options.
  • After uploading the code successfully, I received an IP address on serial monitor, which I then pasted into a new browser tab. This opened up an interface where I could see live footage streaming from the camera.

Reloading the Bootloader: One thing I experienced a few times when programming the XIAO is that the board would occasionally refuse to accept new programming. This usually occurred after I uploaded a program and that didn’t work how I wanted it to!

To resolve this issue, you need to reload the bootloader:

  • Unplug the USB-C cable.
  • Hold down the ultra-tiny Boot button (it has a small “B” beside it)
  • Insert the USB-C cable.
  • Release the button.

XIAO ESP32 S3 Github repo

Files of the week

Codes(.ino)