Skip to content

Commit a392e58

Browse files
committed
STEAM init demo created: oled+wifi scan
1 parent f11c71a commit a392e58

File tree

8 files changed

+280
-156
lines changed

8 files changed

+280
-156
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
"""
2+
FM radio & sensors
3+
4+
This MicroPython script integrates an OLED display, FM radio module,
5+
rotary encoder, PIR motion sensor, and BMP180 pressure sensor. Key
6+
functionalities include controlling the FM radio's volume and frequency,
7+
displaying temperature and pressure data, motion detection with the PIR
8+
sensor, and updating the OLED display with real-time sensor readings.
9+
10+
Authors:
11+
- Tomas Fryza
12+
- Ondrej Kolar
13+
14+
Creation date: 2025-01-23
15+
Last modified: 2025-03-05
16+
17+
Inspired by:
18+
FM RDA5807M
19+
* https://101-things.readthedocs.io/en/latest/fm_radio.html
20+
* https://github.com/franckinux/python-rd5807m/tree/master
21+
* https://github.com/wagiminator/ATtiny85-TinyFMRadio/blob/master/software/TinyFMRadio.ino
22+
* https://github.com/pu2clr/RDA5807/tree/master/examples
23+
24+
Rotary Encoder
25+
* https://techtotinker.com/2021/04/13/027-micropython-technotes-rotary-encoder/
26+
"""
27+
28+
# Micropython builtin modules
29+
import time
30+
from machine import Pin, SoftI2C, RTC, PWM
31+
32+
# External modules
33+
import ssd1306 # OLED display
34+
from bmp180 import BMP180 # Pressure meter
35+
import rda5807 # FM radio module
36+
from rotary_irq import RotaryIRQ # Rotary encoder
37+
38+
LED_PIN = 2
39+
BTN_UP_PIN = 19
40+
BTN_DOWN_PIN = 18
41+
ROT_PIN = 33
42+
ROT_CLK_PIN = 32
43+
ROT_DT_PIN = 35
44+
# BUZZ_PIN = 13
45+
PIR_PIN = 15
46+
47+
48+
def button_mute(pin):
49+
global led, mute
50+
51+
led.on()
52+
time.sleep_ms(20)
53+
mute = not mute
54+
radio.mute(mute)
55+
print(f"Mute: {mute}")
56+
57+
58+
# Init I2C using pins 22 & 21 (default I2C pins)
59+
i2c = SoftI2C(sda=Pin(21), scl=Pin(22)) #, freq=400_000)
60+
print(f"I2C configuration : {str(i2c)}")
61+
62+
# I2C devices
63+
display = ssd1306.SSD1306_I2C(128, 32, i2c)
64+
bmp180 = BMP180(i2c)
65+
radio = rda5807.Radio(i2c)
66+
time.sleep_ms(100) # Let the radio initialize !!! Otherwise the module does not work !!!
67+
68+
# Led & buttons
69+
led = Pin(LED_PIN, Pin.OUT)
70+
btn_up = Pin(BTN_UP_PIN, Pin.IN, Pin.PULL_UP)
71+
btn_down = Pin(BTN_DOWN_PIN, Pin.IN, Pin.PULL_UP)
72+
btn_rot = Pin(ROT_PIN, Pin.IN, Pin.PULL_UP)
73+
74+
# Attach interrupt to the button pin (trigger on falling edge)
75+
btn_rot.irq(trigger=Pin.IRQ_FALLING, handler=button_mute)
76+
77+
# Other devices
78+
# buzzer = PWM(Pin(BUZZ_PIN, Pin.OUT), duty=0) # duty=0 prevents default waveform from starting immediately
79+
pir = Pin(PIR_PIN, Pin.IN)
80+
rot = RotaryIRQ(pin_num_clk=ROT_CLK_PIN,
81+
pin_num_dt=ROT_DT_PIN,
82+
min_val=0,
83+
max_val=15,
84+
pull_up=False,
85+
half_step=True,
86+
reverse=False,
87+
range_mode=RotaryIRQ.RANGE_BOUNDED)
88+
89+
# Set FM module
90+
vol = rot.value()
91+
mute = False
92+
radio_text = ""
93+
radio.set_volume(vol) # 0--15
94+
radio.set_frequency_MHz(103.4) # 103.4 - Blanik
95+
radio.mute(mute)
96+
97+
print("\nStart using FM module and sensors. Press `Ctrl+C` to stop")
98+
99+
try:
100+
# Forever loop
101+
while True:
102+
# Clear display
103+
display.fill(0)
104+
105+
# Volume 0--15
106+
vol_new = rot.value()
107+
if vol != vol_new:
108+
vol = vol_new
109+
radio.set_volume(vol)
110+
print(f"Volume: {vol}")
111+
display.text('vol: '+str(vol), 0, 16, 1)
112+
113+
# Buttons: seek frequency up, down, and mute
114+
if (btn_up.value() == 0):
115+
led.on()
116+
radio.seek_up()
117+
elif (btn_down.value() == 0):
118+
led.on()
119+
radio.seek_down()
120+
else:
121+
led.off()
122+
123+
# BMPT180: air temperature and pressure
124+
pressure = f"{bmp180.pressure/100:.1f} hPa"
125+
display.text(pressure, 60, 16, 1)
126+
temperature = f"{bmp180.temperature:.1f} C"
127+
display.text(temperature, 60, 24, 1)
128+
129+
# Passive infrared, motion detection
130+
display.text('PIR: '+str(pir.value()), 0, 24, 1)
131+
132+
# FM Radio
133+
radio.update_rds()
134+
radio_name = "".join(map(str, radio.station_name))
135+
display.text(str(radio_name), 0, 0, 1)
136+
137+
radio_text_new = "".join(map(str, radio.radio_text))
138+
if radio_text != radio_text_new:
139+
radio_text = radio_text_new
140+
print(f"RDS text: {radio_text}")
141+
142+
display.text(str(radio.get_frequency_MHz())+' MHz', 0, 8, 1)
143+
144+
rssi = radio.get_signal_strength()
145+
display.text(str(rssi)+' dBm', 85, 8, 1)
146+
147+
display.show()
148+
time.sleep_ms(20)
149+
150+
except KeyboardInterrupt:
151+
# This part runs when Ctrl+C is pressed
152+
print("\nProgram stopped. Exiting...")
153+
154+
# Optional cleanup code
155+
led.off()
156+
# buzzer.deinit()
28 KB
Loading

0 commit comments

Comments
 (0)