11. Input devices¶
Welcome to this week’s exploration of input devices. Join us as we uncover the intricacies of sensors, measure electrical outputs, and analyze component behavior. Get ready to dive into the fascinating world of input devices and their role in electronic projects.
Learning Outcomes:
- Measure something: add a sensor to a microcontroller board that you have designed and read it.
Group Assignment Summary¶
For the group assignment this week we tested different sensors and observed their output using an oscilliscope.
Analog Signals¶
Sensor: LDR Sensor¶
The group selected the Light Dependent Resistor (LDR) sensor, which changes its resistance based on the intensity of light it receives. This type of sensor is commonly used in applications like streetlights and camera exposure control due to its cost-effectiveness and reliability. By measuring the ambient light level, the LDR sensor provides an electrical output corresponding to the amount of light detected.
Observed Signals¶
The group connected the LDR sensor to a microcontroller and observed its operation using an oscilloscope. They demonstrated the change in output voltage of the sensor in response to variations in ambient light levels.
Digital Signals¶
Sensor: Ultrasonic Distance Sensor¶
For digital sensor measurement, the group utilized an ultrasonic distance sensor. These sensors use ultrasonic waves to measure the distance between the sensor and an object, finding applications in obstacle detection, level sensing, and proximity sensing in robotics and automation.
Using an oscilloscope, the group measured the voltage output of the ultrasonic analog sensor. They recorded the voltage readings to observe how they changed with variations in the measured distance.
Individual Assignment:¶
For the individual assignment the primary objective is to explore the functionality of different sensors by integrating them with a development board. I’ll be focusing on understanding how to interface various sensors with the microcontroller and program it to read their outputs.
For this assignment, I selected multiple sensors to explore their capabilities. Each sensor serves a specific purpose in the project and offers unique functionalities.
To interface the sensors and facilitate data collection, I will be using a board which I designed in a previous week. This board provides the necessary interface and connections for the sensors, making it easier to integrate them with the microcontroller.
The Seeed Xiao RP2040 microcontroller serves as the brain of the project, handling the data acquisition and processing tasks. Its compatibility with various sensors and ease of programming make it an ideal choice for this assignment.
With this introduction, let’s proceed to explore the integration of each sensor with the development board and microcontroller.
Sensor 1: Piezoelectric Sensor¶
The piezoelectric sensor was chosen for its ability to measure force or pressure applied to its surface. This sensor generates an electrical charge in response to mechanical stress, making it suitable for applications where force sensing is required, such as detecting touch or pressure-sensitive input. Its simplicity, reliability, and versatility make it a popular choice in various industries, including automotive, healthcare, and robotics.
Sensor Setup¶
-
Connect the Sensor: Connect the piezoelectric sensor to the development board using jumper wires.
- Connect the positive (+) terminal of the sensor to an analog input pin on the development board.
- Connect the negative (-) terminal of the sensor to the ground (GND) pin on the development board.
- Power Supply: Ensure that the development board is powered either via USB or an external power source.
Microcontroller Programming¶
-
Arduino IDE Setup: Open the Arduino IDE and select the appropriate board (Seeed Xiao RP2040) and port.
-
Code Structure:
-
Begin by including the necessary libraries for analog input and serial communication.
- Initialize variables and pins for sensor connection.
- In the setup function, initialize serial communication and configure the sensor pin as an input.
- In the loop function, read the analog value from the sensor using the analogRead() function.
-
Print the sensor readings to the serial monitor for testing and debugging.
-
Programming Logic:
-
Read the analog value from the sensor using the analogRead() function.
- Convert the analog value to force or pressure units using appropriate calibration.
-
Optionally, implement threshold detection or filtering algorithms for more precise readings.
-
Code:
// Include necessary libraries
#include <Arduino.h>
// Define variables and pins
const int sensorPin = 26;
int sensorValue = 0;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure sensor pin as input
pinMode(sensorPin, INPUT);
}
void loop() {
// Read analog value from the sensor
sensorValue = analogRead(sensorPin);
// Print sensor readings to serial monitor
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
// Add a delay for stability
delay(100);
}
Testing and Results¶
-
Testing Procedure:
-
Upload the Arduino sketch to the Seeed Xiao RP2040 microcontroller.
- Open the serial monitor in the Arduino IDE to view the sensor readings.
- Apply force or pressure to the piezoelectric sensor and observe the corresponding changes in the readings.
- Results:
Sensor 2: Ultrasonic Distance Sensor¶
The ultrasonic distance sensor was chosen for its capability to measure distances by emitting ultrasonic waves and calculating the time it takes for the waves to bounce back. This sensor is commonly used in applications such as obstacle detection, level sensing, and proximity sensing in robotics and automation. Its accuracy, reliability, and non-contact measurement make it a valuable component in various projects.
Sensor Setup¶
-
Connect the Sensor: Connect the ultrasonic distance sensor to the development board using jumper wires.
- Connect the VCC pin of the sensor to the 3.3V pin on the development board.
- Connect the GND pin of the sensor to the GND pin on the development board.
- Connect the TRIG pin of the sensor to a digital output pin on the development board.
- Connect the ECHO pin of the sensor to a digital input pin on the development board.
- Power Supply: Ensure that the development board is powered either via USB or an external power source.
Microcontroller Programming¶
-
Arduino IDE Setup: Open the Arduino IDE and select the appropriate board (Seeed Xiao RP2040) and port.
-
Code Structure:
-
Begin by including the necessary libraries for controlling the ultrasonic sensor and serial communication.
- Initialize variables and pins for sensor connection.
- In the setup function, initialize serial communication and configure the TRIG pin as an output and the ECHO pin as an input.
-
In the loop function, trigger the ultrasonic sensor, measure the duration of the echo pulse, and calculate the distance based on the speed of sound.
-
Programming Logic:
-
Trigger a pulse on the TRIG pin to start the measurement.
- Measure the duration of the pulse on the ECHO pin to determine the time taken for the ultrasonic waves to bounce back.
-
Calculate the distance using the formula: distance = (duration / 2) * speed of sound.
-
Code:
// Include necessary libraries
#include <Arduino.h>
// Define variables and pins
const int trigPin = 6; // TRIG pin of the ultrasonic sensor
const int echoPin = 7; // ECHO pin of the ultrasonic sensor
long duration;
int distance;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Configure TRIG pin as an output
pinMode(trigPin, OUTPUT);
// Configure ECHO pin as an input
pinMode(echoPin, INPUT);
}
void loop() {
// Trigger ultrasonic sensor to start measurement
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse on ECHO pin
duration = pulseIn(echoPin, HIGH);
// Calculate distance in centimeters based on the duration
distance = duration * 0.034 / 2;
// Print distance to serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Add a delay before the next measurement
delay(100);
}
Testing and Results¶
-
Testing Procedure:
-
Upload the Arduino sketch to the Seeed Xiao RP2040 microcontroller.
- Open the serial monitor in the Arduino IDE to view the distance measurements.
-
Place obstacles at various distances from the ultrasonic sensor and observe the corresponding distance readings.
-
Results: