Halloween Tricks [English Version]




We like pumpkin and for halloween we decide to create with our raspberry pi a motion sensor.

How it’s works ?

The idea is simple, when somebody is in the piece, the lamp is putted on.

Discover Raspberry pi

First step, we install a debian on ourraspberry pi.

Second step, we need to start a lamp with the gpio of the raspberry pi.
GPIO are input output spindle, we can have a high voltage (3.3 V) or low voltage ( 0V ).

We can see on the following picture, how to are the GPIO dispatched.

The first thing we do was to put on a LED. This was done by simply connecting a LED to the GPIO 17 and a resistor in serie.

We define the gpio 17 and we use this as an output :

echo "17" /sys/class/gpio/export
echo "out" /sys/class/gpio/gpio17/direction

And to enable the led, we put the voltage high :

echo "1" /sys/class/gpio/gpio17/value

Note : to put off the led we simply replace « 1 » by « 0 »

Create a motion sensor with a webcam

In practice, we could use a infrared motion sensor. However we only  have a webcam, and it’s more challenging with a webcam.

The code is available in the archive.

int main(int argc, char **argv) {
	// Initialization GPIO 17
	// Image
	IplImage *image1=NULL;
	IplImage *image2=NULL;
	IplImage *subImage=NULL;
	// Capture movie
	CvCapture *capture;
	// Open the webcam with the number of the device (/dev/videoX for X the number)
	capture = cvCreateCameraCapture(atoi(argv[1]));
	//Configure the resolution for the webcam
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);
	cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, 0.65);
	// Check if capture is ok
	if (!capture) {
		printf("Ouverture du flux vidéo impossible !\n");
		return 1;
	printf("Enjoy !");
	// We start our lamp 2 time for check and give the signal
	while(1) {
		// Screen first picture
		image1 = cvQueryFrame(capture);
		// Waiting 50 milisecondes
		// Save image 1
		// Screen second picture
		image2 = cvQueryFrame(capture);
		// Save image2
		// We save both picture
		image1 = cvLoadImage("buffer1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
		image2 = cvLoadImage("buffer2.jpg", CV_LOAD_IMAGE_GRAYSCALE);
		// We substract image1 and image2
		subImage = substract(image1, image2);
		// We count the number pixel difference of the image 1 and image 2.
		int trigger = count(subImage);
		// We compare with our TRIGGER, if we detect mouvement
		if(trigger > TRIGGER) {	
			// if more pixel modify we start the lamp
	// Free capture
	return 0;


Ok, now the program works but we have a problem with GPIO . to put on our lamp we need 5V. For this reason we use a relay in 5V.

However, the raspberry pi ‘GPIO can only provide 3.3V. We need to amplify the signal.

We use the output 5V of the raspberry and an audio amplify (LM386) in non inverting scheme.

We can see the assembly :

Final result

OVERSIMPLE : Halloween détecteur de mouvement par oversimple


Site officiel d’OpenCV

GPIO Raspberry Pi

Codes : complete source code

It’s Oversimple isn’t it?

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.