Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Remember Training Data with a microSD Card (Python)

Remember Training Data with a microSD Card (Python)

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 using learn_id(current_id). The Image.YES icon confirms that the color for that ID has been stored in memory.

  • Logo + Button A pressed:
    The manage_model("save", current_id) command saves the current HUSKYLENS model to the microSD card using the slot number that matches the current ID. The Image.ARROW_N icon appears to indicate that the save was successful.

  • Logo + Button B pressed:
    The manage_model("load", current_id) command loads the saved model from the microSD card back into active memory. The Image.ARROW_S icon 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:
    The forget_all() command clears all learned data from memory. The Image.SKULL icon 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.


Printer-friendly version
Plug in the microSD Card (Python)
Prev
Test the Training Program (Python)
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress