9. Input Devices¶
In this week’s assignment, I worked on understanding and integrating input devices with a microcontroller. The goal was to sense data from the environment using a sensor and process the information.
Understanding the basics¶
What are input devices?
An input device is a component that takes information from the environment and sends it to a microcontroller for processing. These devices can detect things like sound, light, temperature, or motion.
Types of Input devices
- Active Input Devices: Active input devices are sensors that require an external power source to operate. These devices actively send out signals or require power to function and then detect the changes in their environment.
Examples include microphones, ultrasonic sensors, and infrared sensors.
- Passive Input Devices: Passive input devices do not require an external power source to function. Instead, they detect environmental changes and generate signals based on physical interactions.
Examples include light-dependent resistors(LDR), switch, and capacitor
Choosing the input device¶
This week I want to try using the sound sensor. The sound sensor I used is an analog microphone module that detects sound waves and converts them into electrical signals. It works by picking up variations in air pressure (caused by sound) and translating them into a changing voltage output.
The sensor has three main pins:
-
VCC – Power supply (3.3V or 5V)
-
GND – Ground
-
AOUT (Analog Output) – Provides a varying voltage based on sound intensity
Sound sensor¶
For this week assignment make music interactive lights. I was inspired by interactive light installations that respond to music and ambient sounds. Many artists and designers use sound-reactive LEDs in art exhibitions, concerts, and public spaces to create immersive experiences. I wanted to replicate this concept on a smaller scale to understand how it works.
As i was already familiar with Arduino I tried testing the sensor on that first. So based on the intensity of the sound the leds respond to it by turing on or off
I took help of chatgpt to write the code. Prompt: Write Arduino code to control four LEDs using a sound sensor. The sound sensor will control the LEDs based on the detected sound levels. The more intense the sound, the more LEDs should turn on.
Code: Arduino
#define SOUND_SENSOR A0
#define LED1 3
#define LED2 4
#define LED3 5
#define LED4 6
void setup() {
pinMode(SOUND_SENSOR, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop() {
int soundLevel = analogRead(SOUND_SENSOR);
// Adjust threshold values as needed
if (soundLevel > 700) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
}
else if (soundLevel > 600) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
}
else if (soundLevel > 500) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
else if (soundLevel > 400) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
}
Implementing on a Custom Board
Using Xiao RP2040
To do this I will be using the breakout board that i made during week 8
Wire Connection
Sound Sensor → XIAO RP2040
Sound sesor Pin | XIAO RP2040 Pin |
---|---|
VCC | 3.3V |
GND | GND |
A0 | A0 (Pin 26) |
Note: Connect VCC to 3.3V only. The RP2040 is not 5V-tolerant.
LEDs → XIAO RP2040
LED | XIAO RP2040 Pin |
---|---|
LED1 | D2 (Pin 3) |
LED2 | D3 (Pin 4) |
LED3 | D4 (Pin 5) |
LED4 | D5 (Pin 6) |
Code: Xiao RP2040
For this I tweaked the earlier code that i used by upadating the pin assignments and by adjusting the sound level range and then I asked Chatgpt to check my code.
#define SOUND_SENSOR A0 // Pin P26 on XIAO RP2040
#define LED1 D2 // Pin 3
#define LED2 D3 // Pin 4
#define LED3 D4 // Pin 5
#define LED4 D5 // Pin 6
void setup() {
pinMode(SOUND_SENSOR, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
}
void loop() {
int soundLevel = analogRead(SOUND_SENSOR); // Reads 0 - 4095 on XIAO RP2040
// Adjust threshold values based on XIAO's ADC range
if (soundLevel > 700) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
}
else if (soundLevel > 600) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, LOW);
}
else if (soundLevel > 500) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
else if (soundLevel > 400) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
}
}
Learnings: Initially, the code I generated using ChatGPT had an error. I learned that while Arduino Uno allows pin naming using just numbers (e.g., 1, 2,...), the Xiao board requires pin names like D1, D2, etc., due to recent library updates.
How the code works?
Compares Sound Level to Thresholds
- If the sound level is very high (>700), all LEDs turn on.
- If the sound level is moderately high (600-700), three LEDs turn on.
- If the sound level is medium (500-600), two LEDs turn on.
- If the sound level is low (400-500), only one LED turns on.
- If the sound level is very low (<400), all LEDs remain off.
Step Response¶
In addition to using a sound sensor, I explored the step response technique to broaden my understanding of input devices. Step response is not a sensor itself, but a method to create custom capacitive sensors using two conductive plates (e.g., copper).
I referred to Adrian Torres and Robert Hart's documentation to understand how step response works
This method works by sending an electrical pulse from one conductor and measuring the response on another. When an object (like a hand) comes close, the capacitance between the plates changes, which alters the received signal
How It Works?
-
It consists of two conductive plates.
-
One plate sends an electrical pulse, while the other receives it.
-
When an object comes near, the capacitance between the plates changes, altering the response signal.
-
The microcontroller measures this change and processes it to determine proximity or touch.
Note on Noise Filtering
In many step response setups, it's common to add a resistor voltage divider or pull-down resistor between the input and ground. This helps to filter out unwanted electrical noise and stabilize the readings.
Without it, small electromagnetic interference from the environment (like from nearby wires or devices) can cause fluctuating or unstable values in your sensor readings. A common method is to place a 1MΩ resistor between the signal wire (connected to A0) and ground. This helps "pull" the input to a stable state when it's not actively receiving a signal.
I didn’t use this resistor in my setup, but adding one would make the system more reliable, especially if you're building a more permanent sensor.
Testing Step Respose
To test step response, I used two copper plates connected to an Arduino. One plate was connected to pin 4 (output), and the other to A0 (input). The plates act as capacitive sensors, and the system measures the change in electric field coupling between them.
Serial Monitor Test¶
First, I tested the setup using the Serial Monitor in the Arduino IDE. When the copper plates were brought closer together, the numbers displayed on the serial monitor changed significantly.
This confirmed that the setup was successfully detecting changes in capacitance due to proximity. I used chatgpt to write the code. Prompt:
Code
//rx_tx01 Robert Hart Mar 2019.
// Program to use transmit-receive across space between two conductors.
// One conductor attached to pin4, one to A0
//
// Optionally, two resistors (1 MOhm or greater) can be placed between 5V and GND, with
// the signal connected between them so that the steady-state voltage is 2.5 Volts.
//
// Signal varies with electric field coupling between conductors, and can
// be used to measure many things related to position, overlap, and intervening material
// between the two conductors.
//
int read_high;
int read_low;
int diff;
void setup() {
pinMode(4,OUTPUT); //Pin 4 provides the voltage step
Serial.begin(9600);
}
void loop() {
digitalWrite(4,HIGH); //Step the voltage high on conductor 1.
read_high = analogRead(A0); //Measure response of conductor 2.
delayMicroseconds(100); //Delay to reach steady state.
digitalWrite(4,LOW); //Step the voltage to zero on conductor 1.
read_low = analogRead(A0); //Measure response of conductor 2.
diff = read_high - read_low; //desired answer is the difference between high and low.
Serial.println(diff);
//delay(100);
}
Serial Plotter Test¶
Next, I visualized the data using the Serial Plotter in the Arduino IDE. To open the Serial Plotter, go to Tools > Serial Plotter in the Arduino IDE.
Code
// rx_tx01 Robert Hart Mar 2019.
// Modified for Serial Plotter output
// Measures electric field coupling between two conductors.
int read_high;
int read_low;
int diff;
void setup() {
pinMode(4, OUTPUT); // Pin 4 provides the voltage step
Serial.begin(9600);
}
void loop() {
digitalWrite(4, HIGH); // Step the voltage high on conductor 1.
read_high = analogRead(A0); // Measure response of conductor 2.
delayMicroseconds(1000); // Delay to reach steady state.
digitalWrite(4, LOW); // Step the voltage to zero on conductor 1.
read_low = analogRead(A0); // Measure response of conductor 2.
diff = read_high - read_low; // Desired answer is the difference between high and low.
// Output formatted for Serial Plotter
Serial.print("Diff: ");
Serial.println(diff);
delay(100);
}
The graph displayed the variation in capacitance as the plates moved closer or farther apart, showing clear changes in the readings over time.
When the copper plates are away
When the copper plates were touch together.
Observations
- When the copper plates were far apart, the readings remained relatively stable at a lower value.
- As the plates moved closer, the plotted values increased, showing a clear response to proximity.
Exercise files¶
Below are the files for: