Update the Training Programs (Python)
You may have noticed that turning off your robot erased all the HUSKYLENS training data. That can be frustrating! This time, we’ll use the MicroSD card slot on the HUSKYLENS so your training data can be saved and loaded whenever you need it. With this program, you’ll be able to change IDs with the micro:bit buttons, train the camera on colors, save that training to the MicroSD card, and reload it later. You’ll also be able to erase everything when you’re ready for a fresh start.
The Program
- Open learn_colors with the micro:bit Python Editor.
- Update the project name field to learn_colors_with_microsd
- Update the script to match the one below.
- Slick Save.
- Use the Send to micro:bit button to load the program into the micro:bit.

How It Works
The script begins by importing the cyberbot, i2c_repeat, and huskylens modules. These allow the micro:bit to share the I²C communication lines with the cyber:bot and HUSKYLENS, send commands to the camera, and store or retrieve learned color data from a microSD card.
The line i2c_repeat(5, 4).connect() configures the shared I²C bus between the micro:bit and the cyber:bot, with pin 5 used for the clock (SCL) signal and pin 4 for the data (SDA) signal. A short sleep(100) delay gives the system time to initialize.
Next, initialize_i2c() confirms that the HUSKYLENS is connected and ready, and switch_algorithm("color") sets it into color recognition mode. These two lines are essential for communication—without them, the camera won’t respond to learn or save commands.
The variable current_id = 1 starts the color ID counter at 1, and the micro:bit display immediately shows that value. This number identifies which slot the HUSKYLENS will use to learn or recall a color model.
Inside the while True loop, the script continuously checks for button and logo inputs and performs actions based on which combination is pressed:
-
Button A + Button B pressed:
The HUSKYLENS learns the color currently in view usinglearn_id(current_id). TheImage.YESicon confirms that the color for that ID has been stored in memory. -
Logo + Button A pressed:
Themanage_model("save", current_id)command saves the current HUSKYLENS model to the microSD card using the slot number that matches the current ID. TheImage.ARROW_Nicon appears to indicate that the save was successful. -
Logo + Button B pressed:
Themanage_model("load", current_id)command loads the saved model from the microSD card back into active memory. TheImage.ARROW_Sicon confirms that the selected model has been restored. -
Button A pressed alone:
The current ID increases by one each time Button A is pressed. When the counter passes 5, it wraps around to 1. The display shows the updated number so you can see which slot is active. -
Button B pressed alone:
The ID decreases by one each time Button B is pressed. When the counter drops below 1, it wraps back to 5. The display updates accordingly. -
Logo pressed alone:
Theforget_all()command clears all learned data from memory. TheImage.SKULLicon appears to confirm that everything has been erased.
After any button or logo action, the sleep(500) statement adds a short delay that prevents accidental double inputs and gives the display time to update. The display.show(str(current_id)) command refreshes the screen to show the current ID before the next loop begins.
Together, these steps make it possible to train, save, recall, and clear color recognition models directly from the micro:bit interface—without ever needing to retrain after turning the power off. The HUSKYLENS now remembers what it’s learned even after a restart, thanks to the microSD card storage system.
Did You Know
The microSD card turns your HUSKYLENS into a “remembering” AI.
When you use the manage_model("save", id) command, the camera writes its learned model to a slot on the microSD card. Each slot corresponds to an ID, so slot 1 holds what you trained as ID 1, slot 2 holds ID 2, and so on. The data stored there includes the camera’s internal parameters and color profiles, allowing the HUSKYLENS to instantly reload its knowledge later—even after the robot has been powered off.
Volatile vs. nonvolatile memory.
Normally, the HUSKYLENS uses volatile memory—data that disappears when the power is turned off. The microSD card provides nonvolatile storage, meaning the data stays intact even when the device loses power. This is the same principle used in USB drives, solid-state disks, and smartphone memory cards.
Why embedded AI needs this feature.
In robotics and embedded systems, the ability to store and reload trained models is crucial. It allows a robot to “wake up” already trained, ready to recognize its environment without retraining every time it starts. This same idea is used in real-world systems like autonomous drones, smart cameras, and machine-vision factory robots that must remember what they’ve been taught.