Raspberry Pi Mouse Camera Part 2!

366

2

Posted

Introduction: Raspberry Pi Mouse Camera Part 2!

Easy project using a PIR sensor connected to an Arduino Nano which is also connected to a Raspberry Pi to record the video.

So this is the second part of the mouse saga. And this time I have chosen to do things differently! For a start the ultrasonic (boxing ring) sensors have been replaced by one PIR sensor, and that sensor is now connected to an Arduino Nano (via a logic level converter) The Nano takes the input (from the PIR) and after a few seconds checks the raspberry pi is ready to take a video via a GPIO pin from the Raspberry pi, and if the raspberry pi is ready then the Nano will send the raspberry pi a signal to start the video.

The video will then start and run for about 70 seconds. However the Nano will then continue its program and after another few seconds it will turn on the IR light source via the relay. And then after another few seconds it will start generating a colourful pattern around the cup.

Once the video has ended the raspberry pi then sets its GPIO output to high and the nano stops the coloured lights and turns of the IR light, The Nano will then wait for the PIR to be tripped again.

The main reason for taking this raspberry pi and nano approach was because I wanted to use the colourful lights in the middle of the video. I wasn’t sure how easy it would have been to get the raspberry pi to do the lights as well as take the video?

The other bits to this project are a very small LCD display which connects to the Raspberry pi via the 3.5 mm jack. A wireless keyboard is used to start and stop the program. As explained already a Logic level converter is used to connect the PIR to the Nano, this is because the PIR gives out a logic level of 3.3Volts and the Nano needs 5Volts. The logic level converter also safely connects the two pins in and out of the nano to the raspberry pi, one pin on the PI is used as an input to start the camera, and the other pin is used as an output to inform the Nano that firstly it is ready to take a video and then it’s used again to let the nano know when it has stopped the video. The PIR sensor could have been connected directly to the raspberry pi, but I wanted to keep the raspberry pi just for taking the video. The Nano is stuck onto a bit of wood along with the logic level converter, the twin relay board (although only one is used) and a power connector strip where all the 5 volts and grounds are connected. The Nano is powered via a USB lead from the raspberry pi, which means when I have ended the program and turn off the pi the Nano is also shut down.

Step 1:

This will be my last mouse video! I have to be honest that the mice make quite a lot of mess for their size, and more importantly they seem to have a liking for servo leads and have so far managed to eat two wires on a model plane. One wire on a model hovercraft and a servo extension lead, the really bad bit is that they don’t sever the whole cable but just one of the wires on the outer edge, which is either the Signal wire or Ground. And the final straw was the hoover…… I was in my shed for most of the morning making a model hovercraft, now this is made mostly from balsa wood. Balsa wood dust is really annoying and gets right up your nose! So as I am sanding down the model I routinely hoover up the dust. But as the morning went on I kept on getting a bad smell, for quite some time I didn’t work out where it was coming from, and it wasn’t until I finished and had a really good clean up with the hoover did I realise that it was coming from the hoover. When I opened up the hoover I found that most of the bag had been eaten and it was apparent that the mouse/mice had been living in there for some time and had been doing whatever mice do. Needless to say that the more I used the hoover the hotter it became and the stronger the smell got.

Step 2: Arduino Nano Program.

#include 
int ledOutput = 13;
int PIR = 6;
int outputToPi = 3;
int inputFromPi = 2;
int relayOne = 4;
int relayTwo = 5;
int PIRInput = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16 , 7, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  pinMode(ledOutput, OUTPUT);
  pinMode(PIR, INPUT);
  pinMode(outputToPi, OUTPUT);
  pinMode(inputFromPi, INPUT);
  pinMode(relayOne, OUTPUT);
  pinMode(relayTwo, OUTPUT);
  digitalWrite(relayOne, HIGH);
  digitalWrite(relayTwo, HIGH);
  allOff();
  delay(10000);
}
void allOff()
{
  int j =0;
  for(j=0; j< strip.numPixels(); j++)
  {
    strip.setPixelColor(j,0,0,0);
  }
  strip.show();
}
uint32_t Wheel(byte WheelPos) {
  int D = 0;
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, D);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, D, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(D, WheelPos * 3, 255 - WheelPos * 3);
  }
}
void rainbowCycle(uint8_t wait)
{
  uint16_t i, j;

  for(j=0; j<256; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

void loop()
{
  PIRInput = digitalRead(PIR);
  if (PIRInput == HIGH)//pir has tripped
  {
    digitalWrite(ledOutput, HIGH);// turn on 13 led
    delay(3000);//wait 5 seconds to allow swicth off
    int inPi = digitalRead(inputFromPi);
    if (inPi == HIGH)//check pi is ready to take picture
    {
      digitalWrite(outputToPi, HIGH);// tell the pi to start
      delay(3000);// wait 5 seconds to turn on the IR LED's
      digitalWrite(outputToPi, LOW);// reset the start
      digitalWrite(relayOne, LOW);//low is on (ir lights on)
      delay(3000);//record for 30 seconds
      while (digitalRead(inputFromPi) == LOW)//wait for pi to end
      {
        rainbowCycle(20);//turn on disco
      }
      allOff();
      digitalWrite(relayOne, HIGH);//turn off the IR LED
      delay(5000);
      digitalWrite(ledOutput, LOW);
    }
  }
  else
  {
    digitalWrite(ledOutput, LOW);
  }
}

Step 3: Raspberry Pi Program.

import time
import RPi.GPIO as GPIO
import os
import datetime as dt
import sys
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(15 , GPIO.IN)
GPIO.setup(14 , GPIO.OUT)
startVid = 15
vidEnded = 14
pistatus = 14
F = 0
checkFolderBirdCam = os.path.isdir("/home/pi/BirdCam")
if checkFolderBirdCam == False:
    os.mkdir("/home/pi/BirdCam")
    os.mkdir("/home/pi/BirdCam/PICTURES")
    os.mkdir("/home/pi/BirdCam/VIDEO")
           
def checkMedia(camFormat):
    checkMediaUSB = os.path.isdir("/media/pi/KINGSTON")
    if checkMediaUSB == True:#check for usb
        checkFolderBirdCam = os.path.isdir("/media/pi/KINGSTON/BirdCam/VIDEO")
        if checkFolderBirdCam == False:
            os.mkdir("/media/pi/KINGSTON/BirdCam")
        checkFolderPictures = os.path.isdir("/media/pi/KINGSTON/BirdCam/PICTURES")
        if checkFolderPictures == False:
            os.mkdir("/media/pi/KINGSTON/BirdCam/PICTURES")
        checkFolderVideo = os.path.isdir("/media/pi/KINGSTON/BirdCam/VIDEO")
        if checkFolderVideo == False:
            os.mkdir("/media/pi/KINGSTON/BirdCam/VIDEO")
        if camFormat == 'PHOTOS':
            os.chdir("/media/pi/KINGSTON/BirdCam/PICTURES")
        else:
            os.chdir("/media/pi/KINGSTON/BirdCam/VIDEO")
    else:
        if camFormat == 'PHOTOS':
            os.chdir("/home/pi/BirdCam/PICTURES")
        else:
            os.chdir("/home/pi/BirdCam/VIDEO")


while True:
    GPIO.output(pistatus, 1)
    if GPIO.input(startVid)==1:
        checkMedia('VIDEO')
        y = str(F)
        filename = ("vid_")+y+(".h264")
        GPIO.output(pistatus, 0)
        command = ("sudo raspivid -t 60000 -o ")
        print("taking a video ")+filename
        os.system(command + filename)
        os.chdir("/home/pi")
        GPIO.output(vidEnded, 1)
        F = F + 1

Share

    Recommendations

    • Science of Cooking

      Science of Cooking
    • Microcontroller Contest

      Microcontroller Contest
    • Spotless Contest

      Spotless Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments