How to use AI chatbot to code the micro:Bit

Coding Micro:Bit with AI chatbot

I recently had a really interesting experience using ChatGPT to help me code a project in Microsoft MakeCode for the BBC Micro:Bit. In this post, I want to share how ChatGPT helped me troubleshoot a tricky problem and come up with a better solution!

Coding a Robot Dog with MakeCode

I got a request to create code for a robot dog that could be controlled using a mobile app. I jumped into MakeCode and wrote a program that connected the Micro:Bit to a mobile control app via Bluetooth. The setup was pretty straightforward:

  • Set up the servos in their starting positions.
  • Connect to Bluetooth and receive commands.
  • Trigger servo movements based on the received commands.

Everything seemed to be working fine—until I tried running it on a Micro:Bit version 1. It crashed instantly. After some digging, I realized that the problem was memory. The first-gen Micro:Bit only has 16KB of memory, and the Bluetooth library alone eats up 14KB! That left just 2KB for the actual robot control code—not nearly enough.

Switching from Bluetooth to Radio Control

At this point, I needed an alternative. The best option was to ditch Bluetooth and use the Micro:Bit’s built-in radio communication instead. That meant rethinking how the robot was controlled:

  • Replacing Bluetooth commands with radio signals.
  • Using another Micro:Bit as the remote control.
  • Assigning button presses to different movements (e.g., A for left, B for right, A+B for forward).

How ChatGPT Came to the Rescue

I decided to give ChatGPT a shot. I asked it to help me modify my code, switching from Bluetooth to radio control while keeping the basic movement functions intact. The response was amazing—it generated two sections of code:

  1. A controller script, which sends movement commands over radio.
  2. A receiver script, which reads those commands and moves the robot accordingly.

One cool thing I realized is that I could actually upload the same script to both the controller and receiver. The transmitter just sends the signals, and only the receiver executes the movement commands. That made the whole setup way more convenient!

Implementing the ChatGPT-Generated Code

Once I had the new JavaScript code, I pasted it into MakeCode. The best part? MakeCode lets you switch between text and block-based programming, so I could verify the changes easily. I hit upload, tested it out, and—boom—it worked! It usually takes a few tries to get things right, but this time, ChatGPT nailed it on the first attempt.

Final Thoughts

ChatGPT isn’t just a tool for writing text—it’s actually an awesome assistant for programming, especially for beginners. Instead of spending hours debugging, I was able to get my Micro:Bit project up and running much faster. If you’re into coding, especially with Micro:Bit, I highly recommend trying ChatGPT to help with your projects. You might be surprised at how well it works!

Let me know in the comments—have you ever used AI to help with your coding? I’d love to hear about your experiences!