Day 26: How to write code with AI

To enhance their coding skills and streamline project development, the cadets aboard the Nebula Raider will learn how to use ChatGPT to write and optimize MicroPython code. This AI-powered assistant will help them quickly generate code, troubleshoot issues, and explore new project ideas. As a cadet, you will learn to interact with ChatGPT to improve your coding efficiency and creativity.

Components Needed:

  • Raspberry Pi Pico WH Microcontroller
  • Computer or device with internet access
  • Basic electronic components (LEDs, resistors, breadboard, jumper wires)
  • MicroPython environment set up on the Raspberry Pi Pico


Introduction to ChatGPT:

ChatGPT is an AI language model developed by OpenAI that can generate human-like text based on the prompts it receives.
It can help write, debug, and optimize code, as well as provide explanations and ideas for projects.
Setting Up Your Environment:

Ensure you have your Raspberry Pi Pico set up with the MicroPython firmware.
Connect the Raspberry Pi Pico to your computer and open a MicroPython IDE (e.g., Thonny).

Interacting with ChatGPT:

Access ChatGPT through an online platform (such as the OpenAI website) where you can input text prompts and receive responses.
Begin by introducing yourself and explaining your project needs.
Example Prompts:

To get started, here are some example prompts you can use with ChatGPT:

Prompt 1: “Hello ChatGPT, can you help me write a MicroPython code to blink an LED connected to my Raspberry Pi Pico?”

Prompt 2: “I want to create a temperature monitoring system using a DHT11 sensor with my Raspberry Pi Pico. Can you provide the MicroPython code for this project?”

Prompt 3: “Can you help me optimize my existing MicroPython code to make it more efficient?”

Using Generated Code:

Copy the code provided by ChatGPT and paste it into your MicroPython IDE.
Review the code to understand its functionality and make any necessary adjustments for your specific setup.
Upload the code to your Raspberry Pi Pico and test it with the connected components.
Debugging and Optimization:

If you encounter any issues with the code, describe the problem to ChatGPT and ask for troubleshooting advice.
For optimization, you can ask ChatGPT to suggest improvements or more efficient ways to achieve your project goals.

Exploring New Ideas:

Use ChatGPT to brainstorm new project ideas and get detailed instructions on how to implement them.
Ask for explanations on specific topics or components you are unfamiliar with to expand your knowledge.
Example Interaction:

Cadet: “Hello ChatGPT, can you help me write a MicroPython code to blink an LED connected to my Raspberry Pi Pico?”

ChatGPT: “Sure! Here’s a simple code to blink an LED connected to GPIO pin 15 on your Raspberry Pi Pico:

led = Pin(15, Pin.OUT)

while True:
led.value(1) # Turn LED on
sleep(1) # Wait for 1 second
led.value(0) # Turn LED off
sleep(1) # Wait for 1 second

Cadet: “Thank you! How can I modify this code to blink the LED twice as fast?”

ChatGPT: “You can change the sleep duration to 0.5 seconds to make the LED blink twice as fast:

led = Pin(15, Pin.OUT)

while True:
led.value(1) # Turn LED on
sleep(0.5) # Wait for 0.5 seconds
led.value(0) # Turn LED off
sleep(0.5) # Wait for 0.5 seconds

Learning Outcomes:

AI Interaction: Learn how to interact with AI to generate and improve code.
Coding Efficiency: Enhance your coding skills by leveraging AI for quick code generation and troubleshooting.

Project Development: Gain confidence in developing and optimizing projects with the assistance of AI.
By completing this lesson, you will become proficient in using ChatGPT to assist with your MicroPython projects, making you a more efficient and creative coder aboard the Nebula Raider.

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *