Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Raspberry Pi Motion Triggered Halloween Pumpkin Jack-O'-Lantern

My 8 year old son has now completed another Raspberry Pi project. This project is a Halloween Pumpkin, or jack-o'-lantern, which is triggered whenever someone comes near.

Raspberry Pi Motion Sensor (PIR) Halloween Pumpkin Jack-O'-Lantern

It uses a PIR motion sensor to detect someone entering the room and then lights up a sequence of NeoPixels. In this case these are static green and blue NeoPixels, although with a few changes to the code they can light up in any colour, flash or run in any sequence you could wish for.

You can watch my son demonstrating it on the video below.

The project uses GPIO Zero for the PIR Motion Sensor and a simple voltage level shifter to control the NeoPixels.

Lighting up the NeoPixels

I used a long strip of NeoPixels that I happened to have, but you can also use a small strip or large ring for a similar (though less bright) effect.

The NeoPixels do need a little setting up, so I suggest getting these working first before adding the code for the motion sensor. The NeoPixels require a simple electronic circuit and some libraries to be installed which drive the LEDs. This is explained in the worksheet below which covers creating your own circuit on a breadboard. You can also use a PCB such as the MyPiFi NeoPixel board or by creating your own HAT / add-on board for the Raspberry Pi.

I hope to add details of an add-on board in future.

Using the PIR motion Sensor

The motion sensor used goes under a number of different product names including "D-SUN PIR" and is also the one included in the CamJam Raspberry Pi kit. This sensor has 3 pins to connect to the Raspberry Pi: Ground, 5V supply and a Data Out. Although the sensor uses a 5V supply it only gives out a 3V signal so this is directly compatible with the GPIO ports. This has been connected to GPIO 26, which is the physical pin 37. This was chosen to avoid the first 26 ports which were blocked due to the add-on board used.

The motion sensor is easily controlled using the GPIO Zero library which is now standard on the Raspberry Pi.

Source code

The source code to bring this all together is shown below.


from gpiozero import MotionSensor
from neopixel import *
import time

delay = 10
PIRPIN = 26

LEDCOUNT = 150
GPIOPIN = 18
FREQ = 800000
DMA = 5
INVERT = True
BRIGHTNESS = 255


pir =  MotionSensor(PIRPIN)
strip = Adafruit_NeoPixel(LEDCOUNT, GPIOPIN, FREQ, DMA, INVERT, BRIGHTNESS)
strip.begin()

while True:

    pir.wait_for_motion()
    print ("you can't hide")
    for i in range (0,LEDCOUNT,2):
        strip.setPixelColor(i, Color(255,0,0))
        strip.setPixelColor(i+1, Color(0,0,255))
    strip.show()
    time.sleep(delay)
    for i in range (LEDCOUNT):
        strip.setPixelColor(i, Color(0,0,0))
    strip.show()

The code should be placed in a file (eg. pumpkin.py), which then needs to be executed using sudo so that it runs as the root user (adminstrator):

sudo python3 pumpkin.py

Modifications

You will see that when triggered the code sets the NeoPixels to two colours, one for the odd pixels and one for the even ones. You could modify this to use any colour that you wanted and you could even add some code to make the LEDs flash etc. If you create a colour sequence or flashing lights then you can remove the time.sleep entry and have the sensor usable once the light sequence has completed.

You may also want to look at having the program run automatically on startup. This is explained in the following guide under the section "Adding to systemd startup": TightVNC startup with systemd
Note for this you want to change the ExecStart entry to run the above command (without the sudo prefix) and set the user to root.

More Halloween Projects