Friday, 20 March 2026

The "Hug Avoider": A Coding Journey from Blocks to MicroPython

This project journey follows the evolution of a simple social-distancing idea into a fully realized "Hug Avoider" wearable. Written from my perspective, it’s designed to take you from basic block coding into the professional-grade world of MicroPython.



As a maker and educator, I’m always on the hunt for "low floor, high ceiling" projects—those that are easy enough for a beginner to start in an afternoon but deep enough to keep an experienced coder engaged.

I embarked on a multi-stage project using the 4tronix EggBit. What started as a simple social-distancing experiment turned into a  transition from blocks to text-based code. Whether you're a teacher, a parent of a young coder, or a fellow maker, here is the full evolution of the "Hug Avoider."


The Hardware: Meet the EggBit

The foundation of this project is the 4tronix EggBit. It’s a "cute," egg-shaped expansion board for the BBC micro:bit, designed specifically to be wearable.

[INSERT IMAGE: The 4tronix EggBit board showing the front face and sensor mounting]

It breaks out the micro:bit’s pins into a few key components:

  • HC-SR04P Ultrasonic Sensor: These act as the "eyes."

  • LED strip: Perfect for mood lighting or status indicators.

  • A Piezo Buzzer: For audible alerts.

  • Lanyard Hole: Essential for making it a true wearable!

For the makers and "under-the-hood" tinkerers, here is the breakdown of how the EggBit maps to the micro:bit’s pins. This is essential if you plan on writing custom libraries or branching out from the standard extensions.

ComponentHardware Modelmicro:bit PinNotes
Distance SensorHC-SR04P (3V version)

Pin 13 (Trigger)


Pin 14 (Echo)

Uses standard ultrasonic pulse-timing logic.
LED StripAddressable RGB LEDsPin 8Compatible with the neopixel library.
BuzzerPassive PiezoPin 15Can play tones, melodies, or custom frequencies.
Ee
Power3V DCBattery ConnectorPowered via the micro:bit’s battery pack or USB.

Maker Tip: If you're looking to pick one up, you can grab a single EggBit here, or if you're running a classroom or a club, they offer a Three-Pack Special to save a bit of budget.


Phase 1: The "Low Floor" (MakeCode Blocks)

To get the project off the ground, I started with Microsoft MakeCode. This is the perfect entry point for students. Using the dedicated EggBit extension, you can get a "social distancing" alarm running in minutes.

The logic is straightforward: If the ultrasonic sensor detects an object closer than 50cm, the LED strip turns red and the buzzer sounds a warning.


In my Hug Avoider 2 post, I explored how this visual feedback makes the abstract concept of "distance" tangible. It’s a great way to talk about personal space while learning basic "If-Then-Else" logic.


Phase 2: Pulling Back the Curtain (MicroPython)

While blocks are great, they hide the "magic." To really understand how sensors communicate with a computer, I decided to rewrite the project in MicroPython.

For this, I highly recommend using the official MicroPython editor for the micro:bit, which makes flashing your code directly to the board a breeze.

In Hug Avoider 3, we moved away from the "Get Distance" block and wrote our own function see in Phase 3 This is a fantastic STEM lesson in physics and math. 


Phase 3: Handling Real-World Bugs

No project is complete without troubleshooting. In the final phase, I ran into a common maker hurdle: sensor reliability.

When using MicroPython, the timing doesn't need to be precise. If the sound pulse doesn't hit a solid surface (like hitting a soft, sound-absorbing sweater), 

In Hug Avoider 4, I refined the code to include "timeouts" . I found that adding a tiny space utime.sleep_ms(10) between pings . I also added a bit of personality by using the micro:bit’s 5x5 LED matrix to display an "angry" face when someone enters the personal space zone.

from microbit import *
from machine import time_pulse_us
import neopixel, speech

sonar =pin15
sonar.write_digital(0)
fireled=neopixel.NeoPixel(pin13,9)

def rainbow():
    fireled[0] = (255, 0, 40)
    fireled[1]=  (255,165,0)
    #block=yellow
    fireled[2] = (255,255,0)
    #block=green
    fireled[3] = (0,255,0)
    #block=blue
    fireled [4] = (0,0,255)
    # block=indigo
    fireled[5] = (75,0,130)
    # block=violet
    fireled[6] = (138,43,178)
    #block=purple
    fireled[7] = (255,0,255)
    fireled.show()

def blank_it():
    for j in range(8):
        fireled[j] = (63, 0, 0)
    fireled.show()

def howfar():
    sonar.write_digital(1)
    sonar.write_digital(0)

    timeus=time_pulse_us(sonar,1)
    echo=timeus/1000000
    dist=(echo/2)*34300
    sleep(100)
    return dist

def startingMessage():
    mess1 = [
    "This is the hug avoide",
    "please keep back",
]
# Take from https://microbit-micropython.readthedocs.io/en/latest/tutorials/speech.html
    for line in mess1:
        speech.say(line, speed=120, pitch=100, throat=100, mouth=200)
        sleep(500)

def buttonplay():
    if pin12.read_digital()==1:
        #Red Button
        blank_it()
    if pin8.read_digital()==1:
        #Green button
        startingMessage()
    if pin14.read_digital()==1:
        #Yellow button
        rainbow()
    if pin16.read_digital()==1:
        #Blue button
        display.show(Image.ASLEEP)
  
while True:
    buttonplay()
    dist=howfar()
    if dist>30:
        pin2.write_digital(1)
        pin0.write_digital(0)
        display.show(Image.HAPPY)
    else:
        pin2.write_digital(1)
        pin0.write_digital(1)
        blank_it()
        speech.say("back away please", speed=120, pitch=100, throat=100, mouth=200)
        display.show(Image.ANGRY

Why This Project Wins for STEM

This journey from a simple "anti-hug" device to a refined Python-powered wearable covers the entire engineering design process:

  • For Teachers: It’s a perfect curriculum arc. Start with blocks in the junior years and move to the Python distance formula as they progress.

  • For Parents: It’s an affordable, tactile project. Seeing a child wear something they actually coded is the ultimate "lightbulb" moment.

  • For Makers: It’s a great lesson in the limitations of hardware and how to write "defensive" code that doesn't crash when sensors behave unexpectedly.


If you're ready to dive deeper into the code or the specific hardware challenges I faced, check out the original posts here:

  1. The EggBit Hardware Intro

  2. MakeCode Logic & The First Build

  3. Entering the World of Python

  4. Final Refinements & MicroPython Optimization

Happy making—and keep your distance!

All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

No comments:

Post a Comment

The "Hug Avoider": A Coding Journey from Blocks to MicroPython

This project journey follows the evolution of a simple social-distancing idea into a fully realized "Hug Avoider" wearable. Writte...