Tutorial: Working with Sensors
This tutorial covers how to read data from common sensors, process the readings, and make decisions based on sensor input (Hankins & Parslow, 2020).
Analog Sensors
Reading a Potentiometer
A potentiometer is the simplest analog sensor. It outputs a voltage proportional to its rotation:
; Read potentiometer and map to percentage
(WHILE 1
(DO
(SET raw (READ A 0))
(SET percent (MAP raw 0 1023 0 100))
(LOG "Position: " percent "%")
(DELAY 100)))
Reading an LM35 Temperature Sensor
The LM35 outputs 10mV per degree Celsius. Connect its output to analog pin A0.
; LM35 temperature reading
(DEFUN LM35-READ (pin)
(DO
(SET raw (READ A pin))
(SET mv (MAP raw 0 1023 0 5000))
(/ mv 10)))
(WHILE 1
(DO
(SET temp (LM35-READ 0))
(LOG "Temperature: " temp " C")
(DELAY 1000)))
Reading a DHT11 Humidity Sensor
The DHT11 provides both temperature and humidity readings. It requires a specific timing protocol:
; DHT11 reading (simplified)
(DEFUN DHT11-TEMP (pin)
(DO
(PIN-MODE pin 1)
(WRITE pin 0)
(DELAY 20)
(WRITE pin 1)
(PIN-MODE pin 0)
(DELAY 1)
; Read 40 bits of data
(SET raw (DHT-READ pin))
(/ raw 256)))
(DEFUN DHT11-HUM (pin)
(DO
(SET raw (DHT-READ pin))
(% raw 256)))
Digital Sensors
Reading a Button with Debouncing
Mechanical buttons produce noise (bouncing) when pressed. Use software debouncing:
; Debounced button reading
(DEFUN DEBOUNCE (pin)
(DO
(SET v1 (READ D pin))
(DELAY 50)
(SET v2 (READ D pin))
(IF (== v1 v2) v1 0)))
(PIN-MODE D 2 0)
(PIN-MODE D 13 1)
(WHILE 1
(DO
(SET btn (DEBOUNCE D 2))
(IF (== btn 1)
(WRITE D 13 1)
(WRITE D 13 0))))
PIR Motion Detection
The PIR sensor outputs HIGH when motion is detected:
(PIN-MODE D 7 0) ; PIR on pin 7
(PIN-MODE D 13 1) ; LED on pin 13
(WHILE 1
(DO
(SET motion (READ D 7))
(IF (== motion 1)
(DO
(LOG "Motion detected!")
(WRITE D 13 1))
(WRITE D 13 0))
(DELAY 100)))
HC-SR04 Ultrasonic Distance
(DEFUN DISTANCE-CM (trig echo)
(DO
(WRITE D trig 1)
(DLY-US 10)
(WRITE D trig 0)
(SET width (PULSE D echo 30000))
(/ (* width 343) 20000)))
(WHILE 1
(DO
(SET dist (DISTANCE-CM 9 10))
(LOG "Distance: " dist " cm")
(DELAY 200)))
Sensor Fusion
Combine multiple sensors for more reliable readings:
; Average temperature from multiple readings
(DEFUN STABLE-TEMP (pin samples)
(DO
(SET sum 0)
(FOR i 0 samples
(DO
(SET raw (READ A pin))
(SET mv (MAP raw 0 1023 0 5000))
(SET sum (+ sum (/ mv 10)))
(DELAY 10)))
(/ sum samples)))
; Use 10 samples for stability
(SET temp (STABLE-TEMP 0 10))
(LOG "Stable temperature: " temp " C")
Alert System
Create a temperature alert system:
(SET threshold-high 35)
(SET threshold-low 10)
(PIN-MODE D 13 1) ; Warning LED
(PIN-MODE D 8 1) ; Buzzer
(DEFUN CHECK-ALARM (temp)
(COND
((> temp threshold-high)
(DO
(LOG "WARNING: Too hot! " temp "C")
(WRITE D 13 1)
(TONE D 8 2000 500)))
((< temp threshold-low)
(DO
(LOG "WARNING: Too cold! " temp "C")
(WRITE D 13 1)
(TONE D 8 500 500)))
((DO
(WRITE D 13 0)
(WRITE D 8 0)))))
(WHILE 1
(DO
(SET temp (LM35-READ 0))
(CHECK-ALARM temp)
(DELAY 1000)))
References
Hankins, R., & Parslow, P. (2020). Arduino programming with C. Packt Publishing.