Posts tonen met het label led. Alle posts tonen
Posts tonen met het label led. Alle posts tonen

zaterdag 13 september 2014

DIY speed measurement with Raspberry Pi

I am working on a high tech bird house for in my backyard. I am hoping next spring it will be occupied by a couple of great tits.


***********************INTERUPT**************************
 Did I just write 'great tits'? Yes I did, in Dutch it is just a simple 'koolmees', but Google translator actually translates this to 'great tit'. I thought Google was being funny, but when searching images of a great tit, you actually get pictures of great tits instead of .... great tits [link removed by author].
***********************/INTERUPT***********************

Anyway, the plan is that a pair of little great tits start their family in my birdhouse. And I will be on the front row. The birdhouse will be equiped with a night vision camera, a bunch of sensors and a scale to weigh the bird's nest. It is all connected to a Raspberry Pi and made visible on a web site. More will follow this autumn.

One of the features will be a couple of light gates that monitor the birds flying in and out. This setup can also be used for measuring the speed of an object passing each of the light gates. In the demo video you see I can shoot my finger with a speed of 7m/s. That is more then 25km/h or 15mph. I even managed to reach 11m/s once off-camera. So you now know that you can pick your nose and shoot the content with 40km/h (25mph)!

 

The picture below shows the schematics. It has two 3mm IR leds (I don't know the type numbers) that are powered by the 3V3 supply of the Raspberry Pi. The leds don't need to be very powerful because the IR receivers will only be 3cm away. These receivers are BPW85 NPN phototransistors that are sensitive to visible and IR light. The base voltage is dependent of the amount of light that it receives which allows current to flow from collector to emitter. I used a 10K variable resistor to be able to tune the voltage drop to a HIGH level of 3V3 that I can feed to the RPi. So, when the lightpath is blocked it outputs 0V, and 3V3 when the lightpath is not blocked. I found out that when I increased the distance between IR transmitter and receiver to over 4cm the light wasn't strong enough to reach a 3V3 voltage drop. If so, you have to increase the current through the leds or make a buffer (like a Darlington pair) to make it work.


The software is made in Python. The standard way of checking for inputs on the Rpi is with polling (if GPIO.input(x): blablabla). In my previous blog post I got a tip to use edge triggering, which is faster, more reliable and relieves the CPU. Basically the program is always sleeping, only when one of the GPIO inputs goes from a HIGH ('lightpath is unblocked') to a LOW ('lightpath is blocked') level a dedicated function is called. This feature is explained very thorough on Adafruit, but the code below shows the basic example how to use it.

The code waits for a falling edge on both GPIO inputs 2 and 3 (pins 13 and 15). If one the two is triggered, it checks if the other input was already triggered. If this is the case it calculates the time difference in microseconds for a precise measurement. The distance between the light gates is 3cm, which is needed to calculate the speed.

For the upcoming birdhouse application it also checks if the bird entered or left the birdhouse, but for the booger shooter application I suggest you always shoot in one direction, that is AWAY from you.

The code can be found here (link):


zondag 17 augustus 2014

Pi-files: Frontdoor light switch based on local sunset/sunrise

I have a couple of front door LED lights which I would like to switch on automatically during the evening/night. The two conventional methods that are commonly available had their disadvantages:


A timer switch is the easiest and cheapest solution, but doesn't take into account day light savings. Besides that, in Einhoven, the Netherlands where I live in december the sun sets around 16:30 while in June it doesn't get dark before 22:00. A simple timer doesn't take that into account either.

Since you only want the lights on when it gets dark, instead of time you can also use a light sensor to distinguish day and night. You have these front door lights that have this built in. The problem that I had with these devices is that they start bouncing ('flickering') around sunset/sunrise. They constantly turn on/off which causes damage to the LED lights I was using. This cost me already several expensive led lights.

Since I already have a Raspberry Pi installed in the hall way (used for the doorbell alert) I might as well hook up the front door lights. You can find on the Internet all sorts of timetables for the sunset/sunrise, so my Rpi should be able to use that same information as well. A small search on the world wide web led me to a Python package called PyEphem. This package can be used to do astronomical calculations and can tell you for example where a certain object (e.g. the sun) is with respect to the observer (which is me standing in my front yard). Very conveniently it also can calculate for you the upcoming sunset and sunrise. Isn't that exactly what I need?!?

The LED lights are powered with a 12V AC-DC transformer. I put together a small electronics board to be able to use the GPIO's 3V3 of the Rpi to switch the 12V LEDs. I used a IRL3103 N-FET. This is not really a deliberate choice but like most of my transistor/FET choices based on what I have lying around in the lab. Be aware that the N-FET needs to be able to switch with a Gate-Source voltage of 3V3. For example an IRF540 (which I used for a motion detector for a 12V ledbar) doesn't switch with this voltage (Here I solved it by using a smaller BS170 N-FET and use that to switch a IRF 9540 P-FET). I added a manual switch to override the automatic switch in case I just want to turn on the lights. Parallel to the front door LEDs is a test led with a resistor onboard to be able to test the functionality without having to connect the actual front door lights.




I use GPIO pins 18 and 22 to switch the two LED's. You can switch LED's with only one output, but I thought it is a nice additional feature to switch them separately to be able to also create a spooky program for Halloween or something like that.
After including the PyEmphem package I have to indicated where the observer is located. I used itouchmap which can translate an address to a latitude and longitude coordinates. Being an observer in Eindhoven means you are located at [51.5, 5.5]. The PyEphem package calculated the next sunrise and sunset and returns these values. I do this every minute and compare the two; if the next sunset if further away in time than the next sunrise, it means it is night. The other way around means daytime. The LED lamps are switched based on the day/night status.

The code can be found here (link):






donderdag 29 mei 2014

Motion Detection for 12V ledbar

On an electronic flea market I bought two 50cm 12V (700ma) ledbars. They were quite bright and only cost 6 euro a piece. I still needed a proper lighting solution for my work bench and thought these could perfectly serve that purpose.

With some two components conductive glue I glued them on a aluminum strip for thermal conductivity. A wooden frame prevents to look right into the leds when you enter the room. It definitely gives a nice bright even illuminated work bench.

 
I didn't want to use a switch to turn the lights on and off. It is just not cool! Recently I bought 10 PIR motion detection sensor modules (HC-SR501) on ebay for only 10 euro (How can they even make that for one euro?). I thought one of these can do the job for me when I approach my bench.

The PIR sensor module outputs 3V3 when motion is detected. Sensitivity and ON time can be adjusted on the module. It operates on supply voltages between 4.5 and 20V so the 12V (1.5A) adapter I need to power the ledbars can also power the PIR sensor. I wanted to use an IRF540 NFET to switch the leds, but this NFET doesn't open up completely with a Vgs of 3V3 which limits the current flowing through the leds. That is why I used a smaller BS170 NFET that can be triggered with 3V3. R1 is a 10K resistor to prevent a floating gate signal. Due to another 10K resistor (R2) at the drain of the BS170 an inverted signal of the PIR sensor is available, so a PFET (IRF9540) can invert back that signal to switch on the ledbars. For troubleshooting purposes I added a jumper to  put 12V directly on the ledbars and a test led (always add a test led!) to see the circuit works properly even if the ledbars are not connected.


 I stuffed everything into a small plastic storage box that I found in the kitchen and made it possible to rotate the whole box to be able to point the PIR sensor towards the workbench. It turned out to be a very nice local lighting solution. I doesn't turn on when I enter the room or walk around, only when I enter the work bench it turns on. Altogether it didn't cost me more then 15 euros, I don't believe you can buy something like this for so little money.