📡 Reading Sensors
Reading Sensors
Learn how to read input from buttons and sensors - this lets your programs interact with the real world!
What are Sensors?
Sensors are devices that detect things in the real world - like temperature, light, sound, or movement. They send information to your Raspberry Pi so your program can react to what's happening around it!
GPIO Pins: Input vs Output
GPIO pins can be used in two ways:
Output
Send signals OUT (like turning LEDs on/off)
GPIO.setup(pin, GPIO.OUT)
Input
Read signals IN (like reading button presses)
GPIO.setup(pin, GPIO.IN)
Reading Button Input
Buttons are the simplest input device. When you press a button, it connects two wires, sending a signal to your Raspberry Pi.
Basic Button Setup
Understanding the Code:
- GPIO.IN: Sets the pin as an input (for reading)
- GPIO.PUD_UP: Enables pull-up resistor (keeps pin HIGH when button not pressed)
- GPIO.input(2): Reads the current state of pin 2
- GPIO.LOW: Means button is pressed (connected to ground)
- GPIO.HIGH: Means button is not pressed
Button Wiring
How to Wire a Button:
- One side of button: Connect to GPIO pin (e.g., GPIO 2)
- Other side of button: Connect to GND (ground)
- Pull-up resistor: Use
GPIO.PUD_UPin code (built-in on Raspberry Pi)
Note: When button is not pressed, pin reads HIGH. When pressed, it connects to GND and reads LOW.
Reading Button in a Loop
To continuously check if a button is pressed, use a loop:
Continuous Button Reading
Reading Analog Sensors
Some sensors give analog values (like temperature or light level). Raspberry Pi GPIO pins are digital, so we need special techniques:
Temperature Sensors
DS18B20 (digital) or DHT11/DHT22 (digital)
Read temperature values
Light Sensors
LDR (Light Dependent Resistor)
Detect light levels
Distance Sensors
Ultrasonic (HC-SR04)
Measure distance
Project: Interactive Button-Controlled LED System
Let's build a system where pressing a button controls an LED!
Materials Needed:
- Raspberry Pi
- Push button
- LED (any color)
- Resistor (220Ω for LED, 10kΩ for button - optional)
- Breadboard and jumper wires
Wiring Instructions:
- Button: One side to GPIO 2, other side to GND
- LED: Long leg to GPIO 18 via resistor, short leg to GND
- Make sure all connections are secure
The Complete Code:
Understanding the Code:
- led_on variable: Tracks whether LED is currently on or off
- not led_on: Toggles the state (True becomes False, False becomes True)
- time.sleep(0.3): Prevents the button from toggling multiple times from one press (debouncing)
- The loop continuously checks the button and toggles the LED when pressed
Debouncing
When you press a button, it might "bounce" - quickly turning on and off multiple times. This is called "bounce" and can cause your program to think the button was pressed many times. We fix this with a small delay after detecting a press - this is called "debouncing".
Common Mistakes to Avoid
⚠️ Watch Out For:
- Wrong Pin Mode: Make sure to use
GPIO.INfor inputs, notGPIO.OUT - Missing Pull-up: Use
GPIO.PUD_UPto prevent floating pins - No Debouncing: Add a delay after button press to prevent multiple triggers
- Wrong Logic: Remember LOW means pressed when using PUD_UP
- Forgetting Cleanup: Always call
GPIO.cleanup()when done
Summary
You've learned:
- ✅ Sensors let your program detect the real world
- ✅ GPIO pins can be inputs (read) or outputs (write)
- ✅ Use
GPIO.INto set a pin as input - ✅ Use
GPIO.input(pin)to read the pin state - ✅ Buttons read LOW when pressed (with PUD_UP)
- ✅ Always add debouncing delay to prevent multiple triggers
- ✅ You can combine inputs and outputs to create interactive systems
🎮 Try It: Practice Reading Inputs!
Practice writing code to read sensors. Try these challenges:
📝 Challenge 1: Simple Button Check
Write code that reads a button on GPIO 2 and prints "Pressed" or "Not Pressed":
📝 Challenge 2: Button Counter
Write code that counts how many times a button is pressed:
GPIO.IN for reading, GPIO.PUD_UP for pull-up resistor, and add debouncing delays!
🎯 Activity: Interactive Button-Controlled LED System
What You'll Build:
Create a system where pressing a button toggles an LED on and off!
Step-by-Step Instructions:
- Wire the Button: Connect button between GPIO 2 and GND
- Wire the LED: Connect LED to GPIO 18 with resistor
- Write the Code: Use the example code from the Learn tab
- Test: Press the button and watch the LED toggle
- Experiment: Try changing the debounce delay time
- Enhance: Add a second button to control a second LED
Testing Checklist:
- ✅ LED turns on when button is pressed first time
- ✅ LED turns off when button is pressed second time
- ✅ Button doesn't trigger multiple times from one press
- ✅ Program stops cleanly with Ctrl+C
💪 Practice Challenges
Challenge 1: Button State Monitor
Write code that continuously monitors a button and prints its state every second:
Challenge 2: Press Counter
Create a program that counts button presses and displays the count. Reset the count when it reaches 10:
Challenge 3: Hold to Light
Create a system where the LED only stays on while the button is held down:
Challenge 4: Double Button Control
Use two buttons to control one LED:
- Button 1 (GPIO 2): Turn LED on
- Button 2 (GPIO 3): Turn LED off
Challenge 5: Code Detective
What's wrong with this code? Find and fix the mistakes!