Friday, February 3, 2023

73. Using ChatGPT to generate code for the Raspberry Pi Pico



108,991 page views!!

ChatGPT is a wonderful chatbot under development by OpenAI which uses Artificial Intelligence to respond to text input.  It can respond to any request from conversations about almost any subject to writing essays.

It can also write computer code in many languages.

I gave it the following input:

write micropython code for raspberry pi pico to display the following effects on a 60 led strip at pin 0:

Rainbow cycle Theater chase rainbow Twinkle Sparkle Flame Running lights Alternating colors

and it responded with the following code:

from machine import Pin import neopixel import time import random num_pixels = 60 pin = Pin(0) pixels = neopixel.NeoPixel(pin, num_pixels) def rainbow_cycle(wait): for j in range(256): for i in range(num_pixels): pixels[i] = (0, j, 255 - j) pixels.write() time.sleep(wait) def theater_chase_rainbow(wait): for j in range(256): for q in range(3): for i in range(0, num_pixels, 3): pixels[i + q] = (j, 255 - j, 0) pixels.write() time.sleep(wait) for i in range(0, num_pixels, 3): pixels[i + q] = (0, 0, 0) def twinkle(wait): for i in range(num_pixels): pixels[i] = (255, 255, 255) pixels.write() time.sleep(wait) pixels[i] = (0, 0, 0) pixels.write() def sparkle(wait): for i in range(num_pixels): pixels[i] = (255, 255, 255) pixels.write() time.sleep(wait) pixels[i] = (0, 0, 0) def flame(wait): for i in range(num_pixels): pixels[i] = (255, 165, 0) pixels.write() time.sleep(wait) pixels[i] = (0, 0, 0) def running_lights(wait): for i in range(num_pixels): pixels[i] = (255, 0, 0) pixels.write() time.sleep(wait) pixels[i] = (0, 255, 0) pixels.write() time.sleep(wait) pixels[i] = (0, 0, 255) pixels.write() time.sleep(wait) pixels[i] = (0, 0, 0) def alternating_colors(wait): for i in range(num_pixels): if i % 2 == 0: pixels[i] = (255, 0, 0) else: pixels[i] = (0, 255, 0) pixels.write() time.sleep(wait) pixels[i] = (0, 0, 0) effects = [rainbow_cycle, theater_chase_rainbow, twinkle, sparkle, flame, running_lights, alternating_colors] while True: for effect in effects: print("Running effect: " + effect.__name__) effect(0.1)

and added the following information:

This code will run each effect in a loop, printing the name of the effect before it starts. You can adjust

I connected the 60 LED WS2812B strip (Neopixel) to the Pico's TX pin (Pin 0) as in the image below: (I included the push button to allow re-setting of the Pico). 

The Raspberry Pi Pico connected to the GND (black wire) and the signal output (white wire) of the Neopixel strip. The Neopixel strip gets its power from a 5V, 5A exterior supply. The Pico itself is powered through the connector on the right, from the Raspberry Pi 4 (not shown).

The Pico is powered by a Raspberry Pi 4, which comes with Thonny, a Python Integrated Development Environment (IDE). I Installed MicroPython on the Pico and when I fired up Thonny, I copied the code generated by ChatGPT into Thonny and clicked the Run current script button.

And... it worked!!