Thursday, November 21, 2013

More About Relays

To further test my 8-device relay I built a little LED flashlight like this:
Then I experimented with connecting the "to relay" contacts to an actual relay. Here is an enlarged view of a relay connector:
I've added text above the 3 terminals: NO = normally open, NC = normally closed. Kind of obvious from symbol on the board. So, if I wired my flashlight to COM and NC then the light is on initially (and only turned off on purpose).

But wired to NO and COM the light is off to start with and and only on when programmed (note the red lines above). Like these Python statements:

  GPIO.output(RelaySw1,GPIO.LOW) # turn SW on


  GPIO.output(RelaySw1,GPIO.HIGH) # turn SW off

Ok, so the LOW and HIGH still seem backwards. But I don't have to worry about the relays being all on at reboot.

Monday, November 18, 2013

My Opto-coupled Relay Board

First I did the simplest possible test -- wired as shown:

Here's my Python test program:

import time
import RPi.GPIO as GPIO

RelaySw1 = 8 # Pi pin 24

GPIO.setup(RelaySw1, GPIO.OUT)

for x in range(4): # tries
GPIO.output(RelaySw1,GPIO.HIGH) # SW on
GPIO.output(RelaySw1,GPIO.LOW) # SW off


And running it makes the first relay click on and off 4 times. Big deal. Before testing the relay with house current I wanted to take advantage of the current isolation that the board provides. After a few dim attempts I finally found this at an Arduino site:

 If you want complete optical isolation, connect "Vcc" to Arduino +5 volts but do NOT connect Arduino Ground.  Remove the Vcc to JD-Vcc jumper. Connect a separate +5 supply to "JD-Vcc" and board Gnd. This will supply power to the transistor drivers and relay coils. 
NOTE: Each relay draws about .08A (80 Ma) when on, so if all 8 relays are actuated the board needs about 8*80 or 640 Ma (.64 amps). In this case a separate power supply for the relay board is required. Connect as in preceeding paragraph. A 5 Volt 1 A supply such as THIS  would be good.

Here's the rewired image (that works):

Next, I tried controlling it from my 8-port I2C chip (I don't have 8 spare GPIO pins). But as soon as I plugged jumper into the inactive I2C pin, the relay closed. Oops! this board is "active low." I.e., LOW (ground) turns them ON. This is apparently not a "bug" but a "feature." Ok, I can work it backwards but I'm left with a start-up problem. Let's say my 8 relays control 8 irrigation valves. And I never want them all ON at the same time. So: my very first bit of start-up code has to set all 8 pins HIGH.

Tricky (see! 

BTW: My Python program above is all wrong. The "SW on", "SW off" comments are backwards!

I2C -- another day.

Friday, November 15, 2013

36: Capturing a Grayscale Image with the RasPi Camera

The raspistill command offers at least 30 command line options but no option to capture a grayscale image. There are considerable advantages to that mode for using a Pi as a security camera.

1. All of the BW info is contained in each single 8-bit pixel (looking at the Green value only gives you 70%).
2. The BW image takes up 1/3rd as much RAM and when you save an image to the filesystem the same JPEG quality is 1/10th the size of the RGB image.

Here's my little program (which assumes you are passing it an RGB file name):

import StringIO
from PIL import Image
import sys

a = len(sys.argv)
if a != 2:
print "Usage: bw name.jpg\n"

im =[1]).convert("L")
file, ext = sys.argv[1].split('.') + 'BW.' + ext, "JPEG")

Thursday, November 7, 2013

More about the Pi Camera and Python 

Some years ago the NY Times asked actual children to write reviews of childrens' books. The selections were assigned randomly. A 12-year-old girl got a book on snakes. Her review is reproduced here in its entirety: "That book taught me more about snakes than I ever wanted to know."

If you want to mess with image files in Python you might get to feel like the 12-year old. After getting the camera installed you then have to get PIL (as a guess, Python Image Library):

sudo apt-get install python-imaging

Then there's the documentation:

77 pages!

Concerning my experience with the camera:

My Python program executes the "raspistill" app to get an image. I've learned how to examine the image file. If you do this, you want to capture an uncompressed image: use the "-o bmp" format.

Anyway, I can detect motion, adjust for low light (to some extent), snap a higher-res image and email a warning to myself. However, I can't do anything practical with this because of the 6" cable on the camera module -- unless I buy a second Pi dedicated to the camera.

Above, my latest device: an 8-switch opto-isolated relay board. I haven't got anything connected yet but I had it clicking on and off within a few minutes.

Sunday, November 3, 2013

Post 34: The Raspberry Pi Camera

I bought my camera from Newark/Element 14 which is the only place I found the minimum $25 price. Then I followed the installation instructions from--

(others available). I also looked at--

Which includes Python code for comparing images -- as for a security camera. As it happens, their code resembles what I would have done if I'd had to start from scratch -- by comparing the same pixel position in successive low-res images. Since the G (green) part of RGB contains 70% of an image's grayscale information, R and B values can be ignored for a motion detection application. Thanks to the Raspi org I got my camera working with very little learning curve pain.

The way one captures an image is with the command line program "raspistill". This app has no Unix-style man page but if you know to execute--

$ raspistill -?

You'll get by.

In my little test program I wanted to learn just how how much difference to expect when there should have been no change at all. Here are the counts for 5 pair-wise comparisons of 120x90-pixel images:

0  d: 29008
1  d: 19889
2  d: 4704
3  d: 256
4  d: 71
5  d: 25
6  d: 15
7  d: 13
8  d: 8
9  d: 3
10  d: 1
11  d: 5
12  d: 1
16  d: 1

sum: 54000

What this means is that out of the total pixel count of 6 images (54,000) nearly 30k were exactly the same, 20k were off by one, etc.

BTW: the 15cm ribbon cable is a real pain. The only place listing longer cables seems to be--

Only from the Netherlands? Give me a break!