9. Input Devices¶
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
Hero Shot
group assignment:¶
This week, we tested the values of the sliding potentiometer module. Sliding potentiometer module is a common analog signal input component that converts mechanical displacement into voltage signals by changing the resistance value through sliding contacts.
- Firstly, in the Arduino IDE, open the example - Basics - AnalogReadSerial,Select the correct development board and serial port, and then upload the program.
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
-
The function of this program is to read the voltage value (range 0-1023, corresponding to 0V-5V) of the sliding potentiometer connected to the analog pin A0, and print the value through the serial port. Connect one pointer of the oscilloscope to the GND of the sliding potentiometer and the other pointer to the OTB.
-
Oscilloscope settings: Select CH1 and set the voltage range to 10V.
-
There is an interesting experiment: the range of the sliding potentiometer is 0-5V. When you change the resistance of the potentiometer at different times, you will create a curve on the oscilloscope screen.
individual assignment:¶
This week, I learned about basic input devices. Considering my final project, I chose buttons and Pressure sensors as the research content for my input devices.
1.Game button¶
1.1 Know the button¶
-
Button is a very simple type of input device. Its principle is quite straightforward, simply completing or breaking an electrical connection. However, there is a wide variety of keys. This time, I employed a push-button tactile switch. When the switch is pressed, the circuit is conducting; when the finger is released, the circuit is interrupted.
-
The principle of a button: Two conductive metal pins, P1 and P2, are placed very close to each other. When the button is pressed, a component pushes them together, causing the two pins to come into contact and thus completing the circuit.
-
The following two pictures show its dimensions and specifications.
1.2 Connect button¶
-
When the button is not pressed, if the pin is left floating (without a resistor), it may cause unstable pin levels (randomly high or low) due to electromagnetic interference or leakage current, thereby triggering false signals. Therefore, we add a pull-down resistor to the button ————connecting the pin to GND through a resistor (default low level, pulled high when the button is pressed). A 10kΩ resistor is usually used to limit the current and prevent excessive current from flowing through the pin.
-
In my latest circuit board, I have already placed 10k resistors in advance, so I only need to connect the two pins of the button to P0/P2/P4 and the positive pole.
1.3 Button program¶
- Button status detection code :Check whether the button is pressed through the serial port .
const int buttonPin = D0;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
// Print current state (0=LOW, 1=HIGH)
Serial.print("Button State: ");
Serial.println(buttonState);
delay(100);
}
- Button Toggle LED :Although the circuit did not conduct when the button was not pressed. But we can control the LED light to stay on or off by switching the switch state through the number of button presses.
const int buttonPin = 0;
const int ledPin = 1;
int pressCount = 0;
bool ledState = false;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin)) {
delay(50);
while (digitalRead(buttonPin));
pressCount++;
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
}
2.FSR Pressure Sensor¶
2.1 Get to know FSR Pressure Sensor¶
-
RP-W2.5-L15.5 are miniature pressure sensors with a main body width of 2.5mm and a length of 15.5mm. They are in an open circuit state in their natural state and have infinite resistance. When the external pressure reaches a certain value, the sensor triggers and jumps from an open circuit state to a pressure-sensitive conductive state. The output resistance of the lead end changes correspondingly with the change of external pressure, and the larger the pressure, the smaller the resistance value.
-
performance
- Trigger force: 5g
- Pressure sensing range: 5g~1000g
- Pressure application method: Soft contact pressing
- Resistance before triggering: greater than 1M Ω
- Activation time: less than 1mS
- Operating temperature: -20 ° C~+65 ° C
-
Durability: Over 1 million cycles
-
Basic Application Circuit : The usage method is almost the same as that of buttons, and a fixed resistor of about 5-10K ohms can be connected in series.
-
Welding FSR Pressure Sensor: It is very small with only 0.5MM solder legs, making it difficult to weld and requiring great care.
2.2 Connect FSR Pressure Sensor:¶
- The xiao-esp32-C3 I am using has already been soldered with a 10K pull-down resistor in D0/D2/D4, so the sensor only needs to be connected to P2 and VCC.
2.3 program testing¶
- I want to test the FSR sensor feature, and I referred to Deepseek’s help. (Prompt:Please provide the complete code for the ESP32-C3 to read the FSR sensor, requiring the serial port to output the ADC and voltage values; the FSR sensor is connected to D2, and a 10kΩ pull-down resistor is used. )
const int fsrPin = D2; // ADC引脚
void setup() {
Serial.begin(115200);
}
void loop() {
int adcValue = analogRead(fsrPin);
float voltage = adcValue * (3.3 / 4095.0); // ESP32-C3 ADC为12位
Serial.print("ADC: ");
Serial.print(adcValue);
Serial.print(" | Voltage: ");
Serial.println(voltage);
delay(200);
}
- I want to use FSR Pressure Sensor to adjust the brightness of the lights. I asked DEEPSEEK to provide me with some code references. (Prompt: How to use ESP32-C3 to control LED brightness with FSR pressure sensor? Hardware connection: FSR sensor connected to D2 (ADC pin), LED connected to D3 (PWM pin) Functional requirement: The higher the pressure, the brighter the LED; The lower the pressure, the darker the LED; LED turns off when there is no pressure)
const int fsrPin = 2;
const int ledPin = 3;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
int fsrValue = analogRead(fsrPin);
int brightness = map(fsrValue, 0, 4095, 0, 255);
analogWrite(ledPin, brightness);
delay(10);
}
Summary:¶
Through this week’s study, I have learned several different buttons. I also know the important use of buttons in circuits with controllers, and electronic connections are necessary to make the values more accurate. Electronics and programming are crucial knowledge, I hope I can successfully complete the challenge.