---
# System prepended metadata

title: 'Advanced GPIO Control with Python: Sensors, Motors & More'
tags: [Raspberry Pi]

---

**1. Reading Analog Sensors (ADC Required)**
Since [Raspberry Pi](https://www.ampheo.com/c/raspberry-pi/raspberry-pi-boards) lacks analog pins, use an [MCP3008](https://www.onzuu.com/search/MCP3008) ADC (Analog-to-Digital Converter) for [sensors](https://www.ampheo.com/c/sensors) like:

* [Potentiometers](https://www.onzuu.com/category/potentiometers)
* Light-dependent resistors (LDRs)
* [Temperature sensors](https://www.onzuu.com/category/temperature-sensors) ([LM35](https://www.ampheo.com/search/LM35_page6))
![企业微信截图_20250808163654](https://hackmd.io/_uploads/Bk38w4Qdgl.png)


**Wiring**
![企业微信截图_20250808162222](https://hackmd.io/_uploads/ByjR7Emuxx.png)

**Python Code (analog_read.py)**
```
python

import busio
import digitalio
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
import board

# SPI setup
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
cs = digitalio.DigitalInOut(board.D8)
mcp = MCP.MCP3008(spi, cs)

# Analog input on CH0
chan = AnalogIn(mcp, MCP.P0)

print(f"Raw ADC Value: {chan.value}")  # 0-1023
print(f"Voltage: {chan.voltage:.2f}V")  # 0-3.3V
```
**2. Driving Motors**
**A. DC Motor (with H-Bridge [L298N](https://www.onzuu.com/product/stmicroelectronics-l298n-3217273))**
```
python

from gpiozero import Motor
motor = Motor(forward=17, backward=27)

motor.forward(speed=0.5)  # 50% speed
motor.backward(speed=0.3) # 30% speed
motor.stop()
```
**B. Stepper Motor ([ULN2003](https://www.onzuu.com/search/ULN2003) Driver)**
```
python

from gpiozero import PhaseEnableStepperMotor
stepper = PhaseEnableStepperMotor(phase=19, enable=26, steps_per_rev=200)

stepper.forward(speed=0.02, steps=100)  # 100 steps clockwise
stepper.backward(speed=0.01, steps=50) # 50 steps counter-clockwise
```
**3. Hardware Interrupts (Event-Driven Programming)**
Avoid polling loops with interrupts for instant response:

**Button Interrupt Example**
```
python

from gpiozero import Button
from signal import pause

def button_pressed():
    print("Interrupt triggered!")

button = Button(2, pull_up=True)
button.when_pressed = button_pressed  # Attach interrupt handler

pause()  # Keep program running
```
**4. Multiprocessing for Real-Time Control**
Run GPIO tasks in parallel:

```
python

from gpiozero import LED, Button
from multiprocessing import Process

def led_blink():
    led = LED(17)
    while True:
        led.toggle()
        sleep(1)

def button_monitor():
    button = Button(2)
    while True:
        if button.is_pressed:
            print("Button pressed!")

Process(target=led_blink).start()
Process(target=button_monitor).start()
```
**5. Remote GPIO Control (Over Network)**
Access GPIO from another device:

```
python

# Server (Raspberry Pi)
from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory
from time import sleep

factory = PiGPIOFactory(host='192.168.1.100')  # Pi's IP
led = LED(17, pin_factory=factory)

while True:
    led.toggle()
    sleep(1)
```
**6. Essential Debugging Tools**
**1. Check GPIO state:**

```
bash
gpio readall  # Requires wiringPi
```
**2. Monitor system resources:**

```
bash
sudo apt install htop
htop
```
**3. Logic analyzer:** Use PulseView with an FTDI dongle.

**7. Pro Tips**
🔥 Minimize latency: Use pigpio instead of RPi.GPIO for <10μs accuracy.
🔥 Prevent damage: Always use:

* [Resistors](https://www.onzuu.com/category/resistors) for LEDs
* Flyback [diodes](https://www.onzuu.com/category/diodes) for motors
* [Optoisolators](https://www.onzuu.com/category/optoisolators) for high-voltage circuits