Skip to content


Larwm drws radio

Uwch | MakeCode, Python | Cwmpawd, Dangosydd LED, Radio | Cyfathrebu, Dewis, Gweithredwyr perthynol, Magneteg, Synwyryddion, Tonnau radio

Cam 1: Gwneud

Beth yw e?

Larwm di-wifr i'ch rhybuddio pan fydd rhywun yn agor drws – neu'n ei adael ar agor.

How to make it

  • You will need two micro:bits with different code on each. One micro:bit acts as a radio transmitter and the other as a radio receiver.
  • Attach the transmitter micro:bit and battery pack to the corner of a door frame and attach a magnet close to it on the corner of a door.
  • Place the receiver micro:bit anywhere nearby.
  • If the alarm doesn’t work as you expected, you may need to change the magnetic force strength number in the transmitter code. Pressing button A shows the current magnetic force reading. Use this to decide on the threshold number, taking readings with the door open and closed.

Sut mae'n gweithio

  • The transmitter program uses the micro:bit’s compass (magnetometer) input sensor and a loop to measure the strength of the magnetic field every two seconds.
  • It uses selection so when the magnetic field strength falls below a certain level (the threshold), it sends a radio signal ‘open’. If the magnetism reading goes above the threshold, it sends the signal ‘closed'.
  • When the receiver micro:bit receives the signal ‘open’, a cross appears on its LED display and an audible alarm sounds. When it receives the signal ‘closed’, a tick appears on its LED display and no sound plays.

Beth sydd ei angen arnoch

  • two micro:bits and at least one battery pack
  • magned
  • some adhesive putty to fix a magnet to a door and a micro:bit to a door frame
  • if you have a V1 micro:bit and want to hear an audible alarm, optional headphones, buzzer or speaker and crocodile clip leads to attach them

Cam 2: Codio

Synhwyrydd / trosglwyddydd:

1from microbit import *
2import radio
6while True:
7    if button_a.was_pressed():
8        display.scroll(compass.get_field_strength())
9    if compass.get_field_strength() < 100000:
11        radio.send('open')
12    else:
13        display.clear()
14        radio.send('closed')
15    sleep(2000)

Larwm / derbynnydd:

1from microbit import *
2import music
3import radio
7while True:
8    message = radio.receive()
9    if message:
10        if message == 'open':
12  ["C4:4"])
13        if message == 'closed':

Cam 3: Gwella

  • Defnyddio mwy nag un micro:bit i olrhain statws gwahanol ddrysau drwy anfon gwahanol negeseuon radio, e.e. 'drws cefn ar agor'.
  • Defnyddio newidyn i fesur am faint o amser mae drysau'n cael eu gadael ar agor – allai hyn eich helpu i arbed ynni gwresogi?
Ariennir gan Lywodraeth Cymru / Funded by Welsh Government

Translation generously supported by the Welsh Government.