NeoPixels are “sweet” for makers. These vivid, simply =-controllable RGB LEDs energy indicators, mild reveals and vacation decorations the world over. We’ve used NeoPixels with Python to make a Simon sport, Node-RED and even BASIC.
We will management NeoPixels utilizing Raspberry Pi, Arduino and Raspberry Pi Pico W (plus many extra boards) and on this how you can we’ll use CircuitPython on the Raspberry Pi Pico W. CircuitPython has a able to go CircuitPython module that we are able to use to stand up and working with little or no code.
Our objective for this undertaking is to create a vacation themed ornament which makes use of NeoPixels. How we management it’s by way of the Cheerlights API. Cheerlights is a world community of synchronized lights. Sending a tweet to @cheerlights containing one of many supported colours will set off our undertaking to vary coloration, however higher than that, it adjustments the colour of each Cheerlight throughout the globe.
Cheerlights helps the next colours. Within the code now we have transformed these hex values into RGB for ease of use with CircuitPython’s NeoPixel module.
- pink (#FF0000)
- inexperienced (#008000)
- blue (#0000FF)
- cyan (#00FFFF)
- white (#FFFFFF)
- oldlace (#FDF5E6)
- purple (#800080)
- magenta (#FF00FF)
- yellow (#FFFF00)
- orange (#FFA500)
- pink (#FFC0CB)
Constructing the Circuit
For This Challenge You Will Want
The circuit is basically only a NeoPixel ring related to the Raspberry Pi Pico W’s GPIO. We take energy from the three.3V pin, join GND and use GP0 as a knowledge pin to manage the NeoPixels.
|Wire Shade||Raspberry Pi Pico W||NeoPixel|
Constructing the Ornament
We needed to make this undertaking a bit particular, so we designed a fast 3D print to rejoice a vacation season.
Utilizing the identical precept (however massively simplified) as we used for the Tufty 2040 badge holder, we created a primary format utilizing Inkscape and SVG information.
We then imported the undertaking into Tinkercad and extruded the SVG to create a stable 3D object, then added a plinth to include the graphics.
Lastly we minimize sections from the rear to deal with our LEDs and wires. We bought this mistaken however the finish outcome will not be impaired.
1. Go to the official CircuitPython web page for the Raspberry Pi Pico W and obtain the most recent launch UF2 firmware picture. On the time of scripting this was CircuitPython 8 Beta 2.
2. While holding the BOOTSEL button, join the Raspberry Pi Pico W to your pc. A brand new drive, RPI-RP2 will seem.
3. Copy the downloaded CircuitPython UF2 file to RPI-RP2. It will write CircuitPython to the interior flash storage of the Pico W. A brand new drive, CIRCUITPY will seem.
We’d like quite a lot of CircuitPython libraries earlier than we are able to proceed. These libraries of prewritten code add additional options to a undertaking.
1. Obtain the bundle of libraries for a similar model of CircuitPython as put in on the Pico W. We put in CircuitPython 8 so downloaded the bundle for model 8.x.
2. Extract the bundle to your desktop and then open the lib folder contained inside.
3. Copy the next information / folders from this lib folder to the lib folder on the CIRCUITPY drive.
Working with CircuitPython
1. Obtain and set up Thonny in the event you don’t have it already. Thonny is a Python editor which covers Python 3, MicroPython and CircuitPython.
2. Open Thonny and go to Instruments >> Choices.
3. Choose Interpreter, then set the interpreter as CircuitPython, port to computerized, and click on OK. Thonny will now hook up with the Pico W working CircuitPython.
Our undertaking code is made up of two information, secrets and techniques.py and code.py. The secrets and techniques.py file is basically a Python module with two variables that can include the SSID of our Wi-Fi entry level, and the password. It’s best apply to save lots of your Wi-Fi particulars to a separate file known as secrets and techniques.py, this reduces the danger of unintentionally sharing your credentials. This course of works for CircuitPython and MicroPython.
1. Create a brand new file and in there create two objects, ssid and password.
2. For the ssid object, assign it the title of your Wi-Fi entry level / router.
ssid = “YOUR WI-FI AP NAME HERE”
3. For the password, assign the Wi-Fi password.
password = “YOUR SECRET PASSWORD”
4. Save the file to the CIRCUITPY drive as secrets and techniques.py.
Secrets and techniques.py Code Itemizing
ssid = "YOUR WI-FI AP NAME HERE" password = "YOUR SECRET PASSWORD"
The code for this undertaking is contained in a file known as code.py. This file will autorun when the Pico W is powered up, this can be a characteristic of CircuitPython. In MicroPython we might title the file foremost.py to attain the identical outcome. We now begin the method of writing the code that can make up our undertaking.
1. Click on on File >> Open and choose the CircuitPython system. Open code.py on the CIRCUITPY drive. Delete any code within the file.
2. Import modules of pre-written code to deal with pause (time) our code, set an IP handle, use the Pico W’s Wi-Fi chip and to create internet sockets.
import time import ipaddress import wifi import socketpool
3. Import 5 extra modules for safe connections, to make internet requests, a module with our Wi-Fi login particulars,a module to work together with the GPIO and eventually the NeoPixel module.
import ssl import adafruit_requests import secrets and techniques import board import neopixel
4. Create an object, pixel_pin to instruct the code as to the place our NeoPixels are related, then create one other object to set the variety of pixels.
pixel_pin = board.GP0 num_pixels = 7
5. Use one other object, pixels, to configure the NeoPixels related to the GPIO. This object passes the GPIO pin and variety of pixels, saved within the earlier objects and we are able to set the brightness of the pixels. Think about 0.3 as a secure most brightness.
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.1, auto_write=False)
6. Create an object, off to retailer a tuple that comprises three integers, 0,0,0. These integers symbolize the combo of pink, inexperienced and blue mild that we use to make colours with NeoPixels. Three zeros symbolize no mild, basically turning them off.
off = (0,0,0)
7. Use the pixels object, and the worth saved in off to show off the NeoPixels, then use present to set the pixels to that “coloration” and pause for 0.1 seconds.
pixels.fill(off) pixels.present() time.sleep(0.1)
8. Create a operate known as “scare” and set it to simply accept an argument which would be the NeoPixel coloration.
9. Create a dictionary contained in the operate known as “colours” and in there retailer the title and RGB values for the Cheerlights colours. A Dictionary is a Python knowledge storage object which makes use of keys to retrieve values. On this case the keys are the colour names, and when they’re known as, they retrieve the RGB values that our NeoPixels use.
colours = "pink":(255,0,0), "inexperienced":(0,255,0), "blue":(0,0,255), "cyan":(0,255,255), "white":(255,255,255), "oldlace":(253,245,230), "purple":(128,0,128), "magenta":(255,0,255), "yellow":(255,255,0), "orange":(255, 165, 0), "pink":(255, 192, 203)
10. Nonetheless contained in the operate create a conditional take a look at that checks for the colour, handed as an argument within the operate, is current within the colours dictionary.
if coloration in colours:
11. If the colour is current we print the title of the colour, after which set the NeoPixels to that coloration.
print(colours[color]) pixels.fill(colours[color]) pixels.present()
12. If the code will not be current, an error message is printed to the Python shell. This part ends the operate code.
else: print("Sorry that coloration will not be within the record")
13. Hook up with the Wi-Fi utilizing the ssid and password saved within the secrets and techniques module.
wifi.radio.join(ssid=secrets and techniques.ssid,password=secrets and techniques.password)
14. Create a pool of sockets that we are able to use for connections and then create a brand new HTTP session for use when making internet requests.
pool = socketpool.SocketPool(wifi.radio) request = adafruit_requests.Session(pool, ssl.create_default_context())
15. Create some time True loop to run the principle undertaking code. This loop will proceed for so long as the Pico W is powered on.
16. Create an object, feed, to retailer the JSON feed URL for the final coloration tweeted to Cheerlights.
feed = request.get("http://api.thingspeak.com/channels/1417/area/1/final.json")
17. Print the final coloration to the Python shell. That is saved within the JSON object underneath “filed1”.
18. Name the scare operate and move the final coloration, from the JSON feed.
19. Add a ten second pause to carry the NeoPixel coloration earlier than the loop repeats.
20. Save the undertaking to the Raspberry Pi Pico W as code.py. Click on Run to begin the code and watch the NeoPixels change coloration.
21. Use your Twitter account to ship a tweet to @cheerlights, utilizing one of many supported colours.
Full Code Itemizing
import time import ipaddress import wifi import socketpool import ssl import adafruit_requests import secrets and techniques import board import neopixel pixel_pin = board.GP0 num_pixels = 7 pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.1, auto_write=False) off = (0,0,0) pixels.fill(off) pixels.present() time.sleep(0.1) def scare(coloration): colours = "pink":(255,0,0), "inexperienced":(0,255,0), "blue":(0,0,255), "cyan":(0,255,255), "white":(255,255,255), "oldlace":(253,245,230), "purple":(128,0,128), "magenta":(255,0,255), "yellow":(255,255,0), "orange":(255, 165, 0), "pink":(255, 192, 203) if coloration in colours: print(colours[color]) pixels.fill(colours[color]) pixels.present() else: print("Sorry that coloration will not be within the record") wifi.radio.join(ssid=secrets and techniques.ssid,password=secrets and techniques.password) pool = socketpool.SocketPool(wifi.radio) request = adafruit_requests.Session(pool, ssl.create_default_context()) whereas True: feed = request.get("http://api.thingspeak.com/channels/1417/area/1/final.json") print(feed.json()['field1']) scare(feed.json()['field1']) time.sleep(10)