Week 09

Input devices

Learning outcomes

  • Demonstrate workflows used in sensing something with input device(s) and MCU board
Week 3 cover

Assignment requirements

Group assignment

  • Test an input device(s)'s analog levels and digital signals (As a minimum, you should demonstrate the use of a multimeter and an oscilloscope
  • Document your work on the group work page and reflect on your individual page what you learned

Individual assignment

  • Measure something: add a sensor to a microcontroller board that you have designed and read it

Progress status

Group work Done

Test an input device analog levels and digital signals

Individual work In progress

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

Documentation Done

Upload source files.

1) Introduction

Closing gaps

  • Learn about input devices
  • Interact more with Arduino IDE
  • Discuss the group project
  • Develop the individual project
Step right image

2) Group assignment 1 - Test an input devices (analog & digital)

For more details visit Fab Lab Peru Week 9 Group assignment


Project - Ultrasonic Sensor - Digital
Ultrasonic Sensor HC-SR04
Group Assignment - Input Devices (Analog & Digital)
Project idea
  • Ultrasonic HC-SR04 sensor module
  • Measures distance using sound waves
  • Includes transmitter and receiver
  • Range: 2 cm to 500 cm
  • Pins: Vcc, Trig, Echo, GND
Video demonstration

3) Group assignment 2 - Test an input devices (analog & digital)

More information at Fab Lab Peru - Group Assignment: https://fabacademy.org/2026/labs/lima/#page-top

Project - LM 393 Photosensitive light dependent resistor (LDR)
LM 393 Photosentitve light dependent resistor (LDR)
Group Assignment - Input Devices (Analog & Digital)
Project idea
  • Distance detection
  • Driving ability - 15mA with adjustable potentiometer
  • Brightness - Adjustable
  • Detect - brightness and light intensity
  • Analog and digital output
Video demonstration

4) Individual assigment

Problems

Designed microcontroller board doesn't work

Posible solution - make a new board

Step 1 - Protoboard

Mesasure something with the microcontroller Xiao ESP32 C3

Step 2 - Repeat with a new board

Measure and test devices using a multimeter or an oscilloscope

Running 1
Reviewing the Xiao ECP32 C3, technical specitications and connections
This Wiki was useful https://wiki.seeedstudio.com/xiao_esp32s3_getting_started/
Running 2
Select the TCRT5000L reflective optical sensor
Emitter Wavelength: 950 nm - infrared
Running 3
Xiao ECP 32 C3 Connections
Running 4
New installation - download ECP 32 C3 libraries
// TCRT5000L reflective optical sensor
            // Input devices 
            // Board: Seeed Studio XIAO ECP 32 C3

            #define sensor = D6;   // cambia al GPIO/pin que uses

            void setup() {
                  Serial.begin(115200);
                  pinMode(sensor, INPUT);   
            }

            void loop() {
                  int val = digitalRead(sensor);
                  Serial.println(val);
                  delay(100);
              }
          
Swimming 2
Serial Monitor - Light and dark
Swimming 2
Test with three sensors: left - center - right
Camera 1
Connections at Xiao ESP 32 C3 using the protoboard
// Using three TCRT5000L reflective optical sensor
            // Input devices 
            // Board: Seeed Studio XIAO ESP 32 C3

              #define sensor1 D6   // cambia al GPIO/pin que uses
              #define sensor2 D5   // cambia al GPIO/pin que uses
              #define sensor3 D4   // cambia al GPIO/pin que uses

              void setup() {
                  Serial.begin(115200);
                  pinMode(sensor1, INPUT);  
                  pinMode(sensor2, INPUT);  
                  pinMode(sensor3, INPUT);  
              }

              void loop() {
                  int val1 = digitalRead(sensor1);
                  int val2 = digitalRead(sensor2);
                  int val3 = digitalRead(sensor3);
            
              if(val1==1){
                  Serial.println("izquierda");
                  }
              else if(val2==1){
                  Serial.println("centro");
               }
              else if(val3==1){
                   Serial.println("derecha");
              }
              else{
                    Serial.println("error");
              }
              delay(100);
              }

            
Camera 2
Testing devices
Camera 1
Using three TCRT5000L reflective optical sensor & buzzer - Center or not
// Using three TCRT5000L reflective optical sensor & buzzer - Center or not
              // Input devices 
              // Board: Seeed Studio XIAO ESP 32 C3

              #define sensor1 D6   // cambia al GPIO/pin que uses
              #define sensor2 D5   // cambia al GPIO/pin que uses
              #define sensor3 D4   // cambia al GPIO/pin que uses


              void setup() {
                  Serial.begin(115200);
                  pinMode(sensor1, INPUT);  
                  pinMode(sensor2, INPUT);  
                  pinMode(sensor3, INPUT);  
                  pinMode(D3, OUTPUT);
              }

              void loop() {
                int val1 = digitalRead(sensor1);
                int val2 = digitalRead(sensor2);
                int val3 = digitalRead(sensor3);
                if(val1==1){
                  Serial.println("izquierda");
                    digitalWrite (D3, HIGH);
                    delayMicroseconds (5000);
                    digitalWrite (D3, LOW);
                    delayMicroseconds (100000);
                }
                else if(val2==1){
                  Serial.println("centro");
                }
                else if(val3==1){
                  Serial.println("derecha");
                    digitalWrite (D3, HIGH);
                    delayMicroseconds (5000);
                    digitalWrite (D3, LOW);
                    delayMicroseconds (100000);
                }
                else{
                  Serial.println("error");
                    digitalWrite (D3, HIGH);
                    delayMicroseconds (5000);
                    digitalWrite (D3, LOW);
                    delayMicroseconds (100000);
                }
                delay(100);
              }
            

4.1) Additional individual assigment

Details

Designed microcontroller board read real

Showed output - serial monitor valuess

Included code

Demostrate that its works


We repeated the demonstration in Week 10. Input/Output devices were connected.

Running 1
We repeated one process using the Xiao ESP32 C3, HC-SR04 (distance device) and a 1.3" OLED IIC. An Input and output devices were connected. The screenshot shows the verify and upload process.
Running 1
We repeated one process using the Xiao ESP32 C3, HC-SR04 (distance device) and a 1.3" OLED IIC. An Input and output devices were connected. The screenshot shows the verify and upload process.
Running 2
Arduino One screenshot - Verify
Running 3
Serial Monitor with distance detected by the HC-SR04 sensor
Running 4
Input/Output devices connected using HC-SR04 sensor and OLEC IIC
Camera 2
Whole devices and the designed PCB

                    ESP32-C3 (Xiao) + HC-SR04 Ultrasonic + 1.3" OLED I2C
                    -------------------------------------------------------
                    Board   : Seeed Studio XIAO ESP32-C3
                    Sensor  : HC-SR04 Ultrasonic Distance Sensor
                    Display : 1.3" OLED SH1106 I2C

                    HC-SR04 Wiring:
                      HC-SR04 VCC  → 5V
                      HC-SR04 GND  → GND
                      HC-SR04 TRIG → D3 (GPIO3)
                      HC-SR04 ECHO → D2 (GPIO2)  via voltage divider (see below!)

                    !! HC-SR04 ECHO outputs 5V — ESP32-C3 is NOT 5V tolerant !!
                    !! Use a voltage divider on ECHO pin:                       !!
                    !!   ECHO → 1kΩ → D2 (GPIO2)                               !!
                    !!                       └── 2kΩ → GND                     !!

                    OLED I2C Wiring:
                      OLED VCC → 3.3V
                      OLED GND → GND
                      OLED SDA → D4 (GPIO4)
                      OLED SCL → D5 (GPIO5)

                    Libraries needed:
                      - U8g2  by oliver
                  */

                  #include 
                  #include 

                  // ── Pin config ────────────────────────────────────────────────────────────────
                  #define TRIG_PIN  D3    // D3 / GPIO3
                  #define ECHO_PIN  D2    // D2 / GPIO2 (via voltage divider)

                  #define MAX_DISTANCE_CM  400   // HC-SR04 max reliable range

                  // ── Objects ───────────────────────────────────────────────────────────────────
                  U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);

                  // ── Helper: measure distance ───────────────────────────────────────────────────
                  float measureDistanceCM() {
                    // Send 10us trigger pulse
                    digitalWrite(TRIG_PIN, LOW);
                    delayMicroseconds(2);
                    digitalWrite(TRIG_PIN, HIGH);
                    delayMicroseconds(10);
                    digitalWrite(TRIG_PIN, LOW);

                    // Measure echo pulse duration (timeout = max range in us)
                    long duration = pulseIn(ECHO_PIN, HIGH, 30000);

                    if (duration == 0) return -1;  // No echo = out of range

                    float distanceCM = (duration * 0.0343) / 2.0;

                    if (distanceCM > MAX_DISTANCE_CM) return -1;
                    return distanceCM;
                  }

                  // ── Setup ─────────────────────────────────────────────────────────────────────
                  void setup() {
                    Serial.begin(115200);
                    while (!Serial) delay(10);

                    pinMode(TRIG_PIN, OUTPUT);
                    pinMode(ECHO_PIN, INPUT);

                    Serial.println("=== XIAO ESP32-C3 + HC-SR04 + 1.3 OLED ===");

                    u8g2.begin();
                    u8g2.clearBuffer();
                    u8g2.setFont(u8g2_font_ncenB08_tr);
                    u8g2.drawStr(10, 30, "Initialising...");
                    u8g2.sendBuffer();

                    delay(500);
                  }

                  // ── Main Loop ─────────────────────────────────────────────────────────────────
                  void loop() {
                    float distCM = measureDistanceCM();
                    float distIN = distCM / 2.54;

                    // ── Serial output ──
                    if (distCM < 0) {
                      Serial.println("Distance : out of range");
                    } else {
                      Serial.print("Distance : ");
                      Serial.print(distCM, 1);
                      Serial.print(" cm  /  ");
                      Serial.print(distIN, 1);
                      Serial.println(" in");
                    }

                    // ── OLED display ──
                    u8g2.clearBuffer();

                    // Title
                    u8g2.setFont(u8g2_font_ncenB08_tr);
                    u8g2.drawStr(18, 12, "HC-SR04 Distance Fab");
                    u8g2.drawHLine(0, 15, 128);

                    if (distCM < 0) {
                      u8g2.setFont(u8g2_font_ncenB12_tr);
                      u8g2.drawStr(10, 42, "Out of range");
                    } else {
                      char lineCM[20], lineIN[20];
                      snprintf(lineCM, sizeof(lineCM), "%.1f cm", distCM);
                      snprintf(lineIN, sizeof(lineIN), "%.1f in", distIN);

                      u8g2.setFont(u8g2_font_ncenB18_tr);   // big font for cm
                      u8g2.drawStr(0, 42, lineCM);

                      u8g2.setFont(u8g2_font_ncenB12_tr);   // smaller for inches
                      u8g2.drawStr(0, 62, lineIN);
                    }

                    u8g2.sendBuffer();

                    delay(200);  // update 5x per second for smooth readings
                  }
            
Video demonstration

4.2) Additional individual assignment 2

Details

Use your own PCB, show: schematic, working with sensor

Add mesasure reading - voltaje value

Explain analog & digital signals

Add circuit diagram


Running 1
My designed PCB in different versions
Running 2
Schematic PCB on Fusion
Running 3
Schematic PCB with measures and tolerances
Running 4
Measure reading with Sharp sensor - output on serial monitor. Sensor details at: https://naylampmechatronics.com/blog/55_tutorial-sensor-de-distancia-sharp.html
Swimming 2
Multimeter reading - voltages values
Swimming 2
Oscilloscope reading - digital signal using HC-SR04
Swimming 2
Oscilloscope reading - analog signal using Sharp
Swimming 2
Oscilloscope reading - analog signal using Sharp
Swimming 2
Difference between analog and digital signals. Analog is continuous and in this example could take any value between - 5v to 5v. Digital is discrete, generating square waves and values of 0 and 1
Camera 1
Sharp is an analog sensor GP2Y0A02. More details at: https://www.alldatasheet.com/datasheet-pdf/pdf/105513/SHARP/GP2Y0A02.html a
Camera 2
Sharp analog output voltage Vs. Distance
Camera 2
Microcontroller board with project devices
Camera 2
A version of designed PBC
Camera 2
A version of designed PBC
Camera 2
Milling setup
Camera 2
Milling process
Camera 2
PCB afterMilling and Soldering processes

5) Final project advance

Running

Explore devices

Found more band for runners

Swimming

Explore an olympic pool

Devices

Grove Vision AI V2-HX6538 Processor

Camera CSI OV5647

Color and gesture sensor - RGB GY-9960-3.3 APDS-9960

Running 1
Band uses
Running 2
Band
Running 3
Band types
Running 4
Band view
Swimming 2
Pool lines & lanes
Swimming 2
Pool lines & lanes
Camera 1
Grove Vision AI V2-HX6538 Processor, Xiao ESP 32 C3, Camera
Camera 2
Testing devices
Final project page

6) Final results

  • Linked to the group assignment page
  • Documented what you learned
  • Documented your design and fabrication process
  • Explain how your code works
  • Explained any problems and how you fixed them
  • Include original design filess and source code
  • Included a 'hero shot' of your board

7) References files

We learn how to design, make and test a PCB with sensor. Files: in each section

Sections