🎃 Some spooky code samples to hack yourself a pumpkin 👻

Overview

🎃 Tech Or Treat 👻

It's spooky season for those who celebrate Halloween, and to get in the spirit (spirit - get it? 👻 ) we thought it would be fun to hack on some pumpkins and power them with some electronics and IoT goodness!

We'll be live streaming our pumpkin hacks on the Microsoft Reactor, so watch along with us, or catch the recording if you are busy haunting a house or doing the monster mash!

You can register for the live stream at the following link:

Register here

Hacking your own pumpkin

We've included some samples and instructions here should you want to hack your own pumpkin. These samples are in Python and C#.

Show me the samples!

Hardware

We've based these samples on the following hardware:

Parts List:

Proximity Sensors:

Other Ways to Expand Your Project:

  • Smartphone: to display a video or image inside your creation!
  • An Azure Subscription

Setting up your Pi

You can use the Raspberry Pi Imager to install the Raspberry Pi OS if you don't already have it installed. If you want to develop directly on the Pi, you can use the full desktop Raspberry Pi OS, but if you want to develop remotely you can use Raspberry Pi Lite.

Before you start the imaging process, you can configure things like WiFi which makes it easier to connect to your Pi remotely. The way to do this is not obvious currently in the Imager. To bring up the configuration dialog press Ctrl+Shift+X. In this dialog enable SSH so you can remote support your Pi, and configure your WiFi.

To code on your Pi, you can use Visual Studio Code. This can run on your Pi if you want to connect your Pi to a keyboard/monitor/mouse. Alternatively you can connect to your Pi using the Remote SSH extension and develop on VS Code from your PC or Mac. You will need to install the relevant extensions for the programming language you are using.

Wiring WS2812B LEDs

WS2812B LEDs (also known as NeoPixels) are strings of multi-color, fully programmable LEDs. You can connect a string of these to a device like a Pi using the GPIO pins, and control the color of individual LEDs. They are addressable LEDs, so each LED has an address that starts at 0 for first LED on the string. You can have as many LEDs as you want - limited only by the available power supply and the size of an integer! To add more, you attach more to the end of the string, and they automatically pick up the next address.

For example, if you have a string of 4 LEDs, they would be addresses 0-3. Add 4 more on the end, and those would be addresses 4-7. They detect their address automatically, you just need to connect them.

These LEDs run on 5V. Although a Raspberry Pi can provide 5V, the power draw of anything more than a few LEDs is too much for the Pi, and you would risk damaging your Pi. The simplest way to power these is via a 5V power supply, either connected to mains power, or connected to a USB power supply.

The LEDs have 3 connections - 5V, Control and Ground. On the LEDs linked above, there are 2 5V wires and 2 Ground wires. These LED strips are one way, LED stands for Light-emitting diode, and a diode is an electrical component that only allows current to go one way. The strips will be labelled with an arrow showing the direction of the LEDs, the start of the arrow is the start of the strip and the wires you connect your Pi to.

A roll of LEDs with the arrow showing the direction of the LEDs

In the image above there is a roll of LEDs, with the start of the roll showing. This roll starts with connection wires:

  • 3 wires connected to a plug, with a red wire for 5V, green wire for control and a black wire for Ground.
  • 2 wires not connected to the plug, a red wire for 5V, and a black wire for Ground.

The arrow shows the direction of the addresses, so you connect the outer wires at the start of the arrow. You can also cut the strips to a required length. Each LED has connections to the next one that can be cut, and you can use the offcuts in another project by soldering wires onto the exposed connectors.

An LED strip showing the connectors between each LED

In the image above, each LED element in the strip is separated by a black line, and is connected to the LEDs either side with oval copper connectors. To cut the strip, cut along the black line cutting the 3 oval connectors in half. To connect LED offcuts, you can solder wires to the oval connectors for the 5V, control and ground connections.

To wire the LEDs, you need 2 circuits, power and control. The power circuit provides the power to light the LED, the control circuit allows you to control the LEDs.

Power circuit

This is the 5V of power to light the LEDs. For a few LEDs, this can come directly from the Pi, but it is better to use an external power supply. The power supply linked above is great for this, but any 5V power supply with screw terminal connectors will work.

You can use a USB to terminal block cable, and connect these to a USB power supply, but don't use the USB ports on the Pi to power the LEDs. The draw is too high.

Using pin to pin jumper wires, connect the red wire on the connector of the LED strip to the positive terminal of the power supply. Connect the black wire to the negative terminal.

The red wire going into the connector on the LEDs connected the positive terminal of the power supply, and the black wire going into the connector connected to the negative terminal

Control circuit

The control circuit allows your Pi to send messages to the LEDs to control them. This needs to be a compete circuit, from a GPIO pin on the Pi sending the control messages, returning to a ground pin on the Pi.

To connect the control wire, use a pin to connector jumper cable to connect the middle (green) wire on the LED connector block to the GPIO pins.

  • If you are using the Python samples, connect the control wire to pin 18, This is the 6th pin from the SD card slot end on the outside line of GPIO pins.

    The control wire on pin 18 and the ground wire on a ground pin on the Pi

  • If you are using the .NET samples, connect the control wire to pin 10, This is the 10th pin from the SD card slot end on the inside line of GPIO pins.

    The control wire on pin 18 and the ground wire on a ground pin on the Pi

You can see the location of all the pins in the Raspberry Pi GPIO documentation.

To connect the ground to complete the circuit, you need to connect one of the ground GPIO pins to the ground wire of the LED strip. If you are using LED strip linked above, then there is a second ground wire not connected to the LEDs you can use. Strip the end of the black wire, and wrap it around the pin end of a pin to connector jumper wire, fixing it in place with tape. Then connect the other end to one of the Pi's GPIO ground pins, such as the one 3rd from the SD card end of the Pi on the outside edge.

If you are using different LEDs you may need to convert the single ground wire into 2 using multiple wires.

Using Grove sensors

If you want to use Grove sensors with you Pi, you first need to fit the Grove Pi Hat. Fit this onto the Pi over the GPIO pins. You can then connect the LED strip to the pins at the same location on the hat.

The Pi with a Grove hat and the LED pins connected

Once the hardware is installed, you need to install the Grove drivers by following the Seeed Wiki instructions.

Installing your Pi in a pumpkin

The inside of pumpkins is wet, and electronics do not like wet stuff! You should do one of the following:

  • Just put the LEDs inside the pumpkin and leave the Pi outside the pumpkin. You should wrap any bare wires on the LED strip in tape to waterproof them.

    You should only do this if the LED strip is waterproof. The one lined above is waterproof.

  • Wrap everything in a clear plastic bag and seal this against the pumpkin. If you need any holes for power cables and the like, make sure the holes are wrapped in tape to protect them.

You should also make sure that your pumpkin is safe from thieves, wildlife or the elements. We don't want your gadgets getting stolen, eaten by bears or rained on.

Samples

Python samples

These code samples require Python 3 to be installed on your Pi.

You can find all the Python samples in the python folder. Each sample contains 2 files:

  • requirements.txt - a Pip requirements file with all the Pip packages needed for the sample
  • app.py - a Python file containing the code for the sample

When you install the Pip packages, you need to use Python 3 and install using sudo.

sudo pip3 install -r requirements.txt

A default Raspberry Pi OS install may not include Pip. If not use the following command to install everything you need:

sudo apt update && sudo apt install python3-pip python3-dev --yes

To access the LED strips, you need to run the code as sudo.

sudo python3 app.py

The LED samples use the Adafruit NeoPixel library, so in the code you will see each LED referred to as a 'pixel'.

Each LED sample sets the brightness of the pixels, on a scale of 0-1. How bright you can set the pixels depends on what color and the power available. If the brightness is too high for the available power, the pixels will not light up. If you need brighter pixels, you may need a higher amperage power supply. For example, using a USB terminal block with a 1A USB plug won't support as high a brightness as a 2.5A power block. It is advisable to use a power pack like the one linked above that supports higher amperages if you want to light long strips at full brightness.

If your pixels don't light up, as well as checking the power and connections, also try a lower brightness value.

Sample Description
single-pixel Lighting a single pixel
multiple-pixels-same Lighting multiple pixels in the same color
multiple-pixels Lighting multiple pixels in different colors
animated-pixels Lighting multiple pixels to animate colors
spooky-sounds Playing spooky sounds through a speaker
distance-sensor How to detect movement using a distance sensor
internet-control Control your pumpkin via Azure IoT Central

.NET samples

You can find all the .NET samples in the dotnet folder.

These LEDs are often referred to as NeoPixels, and the Ws2812b refers to each LED as a pixel. You will see this naming convention used in the code.

Sample Description
get-started Getting started with LEDs by lighting multiple pixels in the same color
bouncing A bouncing light
wave Light the lights in a sine wave
image-scan Change the color of the lights to follow the colors in an image
You might also like...
This repository hosts the code for Stanford Pupper and Stanford Woofer, Raspberry Pi-based quadruped robots that can trot, walk, and jump.
This repository hosts the code for Stanford Pupper and Stanford Woofer, Raspberry Pi-based quadruped robots that can trot, walk, and jump.

This repository hosts the code for Stanford Pupper and Stanford Woofer, Raspberry Pi-based quadruped robots that can trot, walk, and jump.

New armachat based on Raspberry Pi PICO an Circuitpython code

Armachat-circuitpython New Armachat based on Raspberry Pi PICO an Circuitpython code Software working features: send message with header and store to

Code for the onshape macropad.
Code for the onshape macropad.

Onshape_Macropad Code for the onshape macropad. This is a macropad built using the Pimoroni Keybow and the KPrepublic Enclosure. pimoroni_keybow kprep

♟️ QR Code display for P4wnP1 (SSH, VNC, any text / URL)
♟️ QR Code display for P4wnP1 (SSH, VNC, any text / URL)

♟️ Display QR Codes on P4wnP1 (p4wnsolo-qr) 🟢 QR Code display for P4wnP1 w/OLED (SSH, VNC, P4wnP1 WebGUI, any text / URL / exfiltrated data) Note: Th

This repo uses a stereo camera and gray-code-based structured light to realize dense 3D reconstruction.
This repo uses a stereo camera and gray-code-based structured light to realize dense 3D reconstruction.

Structured-light-stereo This repo uses a stereo camera and gray-code-based structured light to realize dense 3D reconstruction. . How to use: STEP 1:

Example code to sending USB Gadget multimedia keys via Python

Send Multimedia USB HID Keys via Python As an USB Gadget in Linux This gives a simple script with zero dependencies that can easily run on any Linux d

Example Python code for building RPi-controlled robotic systems

RPi Example Code Example Python code for building RPi-controlled robotic systems These python files have been compiled / developed by the Neurobionics

Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment

Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment. On row of LEDs shows the cost of the hour, the other row the cost of the day.

Doughskript interpreter for converting simple command sequences into executable Arduino C++ code.

Doughskript interpreter for converting simple command sequences into executable Arduino C++ code.

Comments
  • Add image-scan demo

    Add image-scan demo

    This introduces a quick image-scan effect that samples a horizontal group of pixels from an image and uses that to generate an animation. The sample expects an IMAGE_URL and will download the image before beginning the effect. You can provide a link to a PNG or JPEG. The animation wraps back to the top after finishing the image so to achieve a smooth animation select a texture that wraps vertically.

    You will need to install libgdiplus for this to work: apt-get update && apt-get install -y libgdiplus.

    This sample can render non-mathematical effects that emulate the texture of the image you use.

    If you prefer static lighting you can provide an image that is only 1 pixel high.

    opened by runewake2 0
  • Set LED colors with .NET

    Set LED colors with .NET

    This is a .NET 5 sample that sets the colors of an LED strip to a single color. Intended as an intro project using .NET.

    Outstanding work: add a README with running instructions. (dotnet publish -r linux-arm will create the binary then just publish it with scp or similar). Note: dtparam=spi=on must be set in the Pi config.

    opened by runewake2 0
Owner
Jim Bennett
I do things with IoT and academic advocacy in the Developer Relations team at @Microsoft. He/Him
Jim Bennett
Samples for robotics, node, python, and bash

RaspberryPi Robot Project Technologies: Render: intent Currently designed to act as programmable sentry.

Martin George 1 May 31, 2022
Testing out some (stolen) DMA code for RP2040 Micropython

RP2040_micropython_dma testing out some (stolen) DMA code for RP2040 Micropython. Heavy inspiration and some code from https://iosoft.blog/2021/10/26/

null 2 Dec 29, 2022
Code reimplementation of some papers published in SAIL-Lab

SAIL SAIL-Lab统一代码库 Motivation 创建这个项目的动机最早来源于实验室组内成员相互Debug代码的时候遇到的麻烦。

Jianwen Chen 8 Nov 15, 2022
Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi

Clean Dashboard for Pi-Hole Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi.

Alessio Santoru 104 Dec 14, 2022
Run this code to blink your ThinkPad LED with a hidden mysterious Morse code! ;)

TMorse Run this code to blink your ThinkPad LED with a hidden mysterious Morse code! ;) Compatible with python3.9+. No third-party library is required

Mahyar 2 Jul 11, 2022
Raspberry Pi Pico support for VS Code

Pico-Go VS Code Extension Pico-Go provides code auto-completion and allows you to communicate with your Raspberry Pi Pico board using the built-in REP

Chris Wood 114 Dec 28, 2022
This repository contains all the code and files needed to simulate the notspot quadrupedal robot using Gazebo and ROS.

Notspot robot simulation - Python version This repository contains all the files and code needed to simulate the notspot quadrupedal robot using Gazeb

null 50 Sep 26, 2022
Simple Python script to decode and verify an European Health Certificate QR-code

A simple Python script to decode and verify an European Health Certificate QR-code.

Mathias Panzenböck 61 Oct 5, 2022
Python code written to utilize the Korlan usb2can hardware to send and receive data over the can-bus on a 2008 Nissan 350z

nissan_ecu_hacking Python code written to utilize the Korlan usb2can hardware to send and receive data over the can-bus on a 2008 Nissan 350z My goal

Liam Goss 11 Sep 24, 2022
Example code and projects for FeatherS2 and FeatherS2 Neo

FeatherS2 & FeatherS2 Neo This repo is a collection of code, firmware, and files

Unexpected Maker 5 Jan 1, 2023