9. Input Devices¶
Assignment:¶
Group assignment:¶
- Probe an input device’s analog levels and digital signals.
Individual assignment:¶
- Measure something: add a sensor to a microcontroller board that you have designed and read it.
Group Assignment Highlight:¶
Probing Input Device Signals
1. VL53L1X Distance Sensor with Logic Analyzer
- Sensor Overview: Time-of-Flight (ToF) sensor with I2C communication, 4m range, and 50Hz update rate.
- Setup: Connected to a custom PCB (SDA, SCL, VCC, GND) and programmed via Arduino IDE using the VL53L1X library.
- Logic Analyzer (Saleae Logic 8):
- Captured I2C signals (SCL & SDA) to monitor communication between the microcontroller and sensor.
- Used the I2C decoder in Saleae software to interpret data packets.
- Observed real-time distance measurements and signal integrity.
2. Potentiometer PWM Signal on Oscilloscope
- Setup: Read analog voltage from a potentiometer, mapped it to PWM (0–255), and output to an LED.
- Oscilloscope: Visualized PWM duty cycle changes as the potentiometer knob was turned, confirming analog-to-digital conversion.
Key Takeaways
- Digital Signals (I2C): Logic analyzers decode protocol-level communication (e.g., register reads/writes).
- Analog Signals (PWM): Oscilloscopes visualize voltage variations and timing (e.g., duty cycle modulation).
Both methods are essential for debugging and validating input device behavior.
Tools Used: VL53L1X, Saleae Logic 8, Oscilloscope, Arduino IDE.
Here is the link to the group Assignment.
Individual Assignment:¶
List of Sensor that I decided to explore this week are: - Ultrasonic Sensor - Button - Potentiometer
Ultrasonic Sensor¶
I refer this website to get the details of Ultrasonic Sensor.
An ultrasonic sensor is an instrument or an electronic device that measures the distance to target object using ultrasonic sound waves. It ultrasonic sensor uses a transducer for sending and receiving the ultrasonic pulses which delivers us information about proximity of an object. An ultrasonic sensor transforms the sound reflected to an electric signal. Ultrasonic waves would travel faster compared to the speed of sound which is audible to humans.
The ultrasonic sensor has two components namely, - transmitter - receiver
Transmitter uses piezoelectric crystals, the transmitter emits the sound. Receiver finds the sound after it is traveled to and from the target.
For calculating the distance between the sensor and object, sensor measures the time it takes for the transmitter to emit the sound and to its contact with receiver.
D = (T*C)/2
where D = distance, T = time, C = speed of the sound
The fundamental working principle of ultrasonic sensor is to measure the time it takes for the signal sent by a transmitter and sent back or propagated back to the receiver. It is evident from the name ultrasonic sensor that it operates on the ultrasonic frequencies. Ultrasonic frequencies ranges is beyond our audible sound to humans and these frequencies are greater than 20 kilo Hertz. Ultrasonic sensor is versatile in sensor technology and are used significantly in many industrial applications. There re various types of objects that can detected such as solids, liquids, granules and powder. They detect the transparent objects or shiny objects, as well as objects for which the color changes.
To work with Ultrasonic sensor, I connect the ultrasonic with my microcontroller board that I desiged and fabricated. Then I upload the example code that comes with Ultrasonic sensor library.
- Here is the clean circuit diagram
Wiring: Ultrasonic Sensor -> NAP-XIAO: - VCC -> 5VDC - TRIG -> Pin 27 - ECHO -> Pin 26 - GND -> GND
This the code that I used and it copied from here.
/*
* Created by ArduinoGetStarted, https://arduinogetstarted.com
*
* Arduino - Ultrasonic Sensor HC-SR04
*
* Tutorial is available here: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor
*/
int trigPin = 27; // TRIG pin
int echoPin = 26; // ECHO pin
float duration_us, distance_cm;
void setup() {
// begin serial port
Serial.begin (9600);
// configure the trigger pin to output mode
pinMode(trigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(echoPin, INPUT);
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(echoPin, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
// print the value to Serial Monitor
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
delay(500);
}
How does the above program work?
This code is ultrasonic distance measurement system using an HC-SR04 sensor. The trigPin
sends a 10-microsecond pulse to trigger the sensor, and the echoPin
measures the duration of the returning pulse. The duration is then converted to distance in centimeters using the formula distance_cm = 0.017 * duration_us
(since sound travels at approximately 0.017 cm/µs). The measured distance is printed to the Serial Monitor every 500 milliseconds.
Button¶
These small sized switches are placed on PCBs and are used to close an electrical circuit when the button is pressed by a person.
When the button is pressed, the switches turn ON and when the button is released, the switches turn OFF.
A tactile switch is a switch whose operation is perceptible by touch.
There are two types of Tactile Switches: Tactile Switches and Sealed Tactile Switches.
They are further sub-categorized into PCB Insert Hole Terminals Types or PCB Surface Mounted Terminals Types, depending on how the switch is mounted to a PCB.
For test the switch integrated on my board, I simply connect led to the pin D9 where my button with pull up resistor is integrated. When I pressed the button, 3V from one side of the switch get connect to the led side of connection.
Potentiometer¶
I explore about this sensor from this website.
A potentiometer is a variable resistor with three terminals whose voltage is adjustable manually with the help of a movable contact, in order to control the flow of electric current through it. Every variable resistor will have some kind of mechanical or electronic control to vary its resistance, based on the variation of this resistance the voltage across it and current through it is controlled with respect to Ohms Law. The most obvious use of the potentiometer which most of us have spotted is volume control in radios and other audio equipment.
A Potentiometer (or) POT is a passive electronic component that has two end terminals with a resistive element and the sliding contact called the wiper acts as the third terminal. The input voltage is applied across the two end terminals and the output is taken between the first end terminal and the sliding contact. The output voltage will be taken across any one of the two input terminals and the sliding contact. The POT helps us to use variable voltages in the same device.
To work with Potentiometer, I made a circuit using potentiometer, two leds and my microcontroller board. I used leds so that I can visually see the varying resistance affecting the brightness of the leds.
The circuit: - potentiometer connected to analog pin A0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground - LED connected from digital pin 9 to ground through 220 ohm resistor
This is the example code I used. I have only changed the pins for led to 27 as I will be using this pin for led.
/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255 and uses
the result to set the pulse width modulation (PWM) of an output pin.
Also prints the results to the Serial Monitor.
created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInOutSerial
*/
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 27; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the Serial Monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(2);
}
How does the above program work?
This code reads an analog input from a potentiometer connected to pin A0
and uses it to control the brightness of an LED connected to a PWM-capable pin (analogOutPin = 27
). The analogRead()
function reads the potentiometer value (0 to 1023), which is then mapped to a range of 0 to 255 using the map()
function. This mapped value is written to the LED using analogWrite()
, adjusting its brightness. The sensor and output values are printed to the Serial Monitor, and a 2-millisecond delay ensures stable readings.