Tag Archives: grovepi

Sensors and Relays, Raspberry Pi, and Python

6 Jan

My school has a Computer Programming/Raspberry Pi club that my dad and I host.  Each time, we teach a lesson about the Pi, Python, Scratch, code.org, binary/hex, etc. and then students break into groups to learn about a topic of their choice.  Some choose to go deeper into Scratch, others work on Raspberry Pi projects, and others use websites like codeacademy or EdX to go learn a different programming language.  We spent a couple of classes talking about what the different languages are used for.  For example, we said that HTML and CSS are good for making websites, Python is good for making MineCraft hacks on the Raspberry Pi, and Javascript is good for making web apps.

We’ve also had several guest speakers who are experts in computer programming come to our school and speak.  For example, we had a Google intern, we went and visited Google, and we had a programmer from JennaSys Engineering speak to us.  We asked each one what programming language they would recommend that we start with.  The Google Intern said “Python or Java,” the Google engineer said “Anything but Python, it’s too far from the hardware,” and the JennaSys engineer said, “Python.”  So, we decided that our lessons would focus on Python.

Below is the script that I wrote for our most recent lesson.  For this lesson, we used a GrovePi to take input from a light sensor and send output to a relay.  The circuit spins a motor if the light level gets too low.  So, this could be used to open and close your window blinds as the sun rises and sets, but that’s not very realistic.  In reality, there are many, many devices that use this exact same structure.  For example, your thermostat turns on the heater if the temperature gets too low and the AC if the temperature gets too high.  Some cars turn on the headlights when it gets too dark.  Your refrigerator compressor kicks in if the fridge gets too warm.  Some cars have windshield wipers that turn on when they detect moisture.  Grocery store doors open when they detect a person nearby.  An alarm goes off when a door is opened.  What these systems all have in common is that there is a sensor that triggers a relay to do something.  There are hundreds of other examples of this, too (3D printer micro switch, printer jam detection, phone overheat protection, laptop turns off when the lid closes, etc.) so we decided to look at an example of code that can make this happen.

I have to put a picture of the code because my spacing in the program gets messed up on WordPress for some reason:

SensorCode2

A “relay” is a switch that you can turn on and off with code.  The click that you hear when you turn on a device with a remote control is the relay switching on.  They have one side that is “Normally Open” and you can use code to close it (turn it on) and another side that is “Normally Closed” and you can use code to open it (turn it off).  It gets confusing because generally, when you send a value of “1” to an output, you are turning it on.  But if that device is plugged into the “Normally Closed” side of the relay, then it’s already on and you turn it off by sending the value of “1.”

So, although the circuit that we had that day was only slightly cool, the applications to other areas are powerful.  This project also demonstrates why a Google engineer told us that Python isn’t the best language to learn.  The libraries make it so easy.  If you were writing code for a car’s computer, you wouldn’t be able to take the shortcut by importing the GrovePi library.

Materials:

Raspberry Pi Starter Kit: http://amzn.to/13UtN2F  $69.95

GrovePi board and sensors: http://amzn.to/1tEdgad  $75.00

DC Motor: http://amzn.to/1tKqzvg  $5.00

Variety of batteries, clips, and wires

Advertisements

My GrovePi Adventure

19 Nov

Here we go again . . . another story of following the instructions for a project exactly and failing miserably.  A while back, I got a GrovePi and a bunch of sensors.  It would be perfect for adding inputs to the Raspberry Pi since it has temperature, pressure, flame, light, sound, knock, distance, and more sensors.  So, first, I decided to try the joystick.  I went to the website for GrovePi and found instructions there (dexterindustries.com or seedstudio.com).  Wait . . . what?  The kit didn’t come with any cables.  Huh?  The one in the picture has a white plastic connector with four pins.  Mine has no connector and 5 pins!  What’s going on here?

I found out that the sensors that I bought on Amazon are not GrovePi sensors at all.  They are compatible . . . sort of, but they are not the GrovePi sensors.  They came with some connector wires (that don’t fit) and a ribbon cable (which is pretty useless).  The little instruction book only has a picture of each sensor and no instructions whatsoever.  There is an instruction manual online, but it sounds like it was written in another language and translated by a 4-year old.  Here are a couple of quotes:

“13 comes with digital interfaces of the LED, the shock sensor access number 3 interface, when a sense of shock sensor”

“The reason why we feel that infrared is really a wonderful thing, it is because we are invisible, intangible, but Okay, we do not need that, too, can control it and make it serve us, in fact, we are more magical, Is not?”

“The amount of points we can hand to block receiver module, see also the normal communication between them do? The following is the receive window Ah, looked at the window, and we all know it. . .”

I don’t mean to make fun of anyone and their English ability, but these sentences are so confusing that I cannot figure any of them out.  And it’s only written for Arduino (yuck!), not Raspberry Pi.

The reason for all of this is that I purchased a kit on Amazon called the SunFounder 37 modules Raspberry Pi Sensor Kit.  This kit is no longer available, but there are still Arduino kits like this one.  You can still get the old kit at Amazon.de, but I wouldn’t recommend it.

Bottom line is that the GrovePi is a great product.  It has solved all of the issues that I blogged about before with using analog sensors on the Raspberry Pi (here).  The GrovePi library has all of the code for accessing the analog ports on the GrovePi with simple code.  But I would highly recommend buying actual GrovePi sensors (like this one), not SunFounder sensors.  Support for GrovePi sensors on the Raspberry Pi is pretty good, but the software comes with sample code that can easily be modified to use most of the sensors and there are a few cool projects on their website (here).  (Note: This kit no longer says on the advertisement that it works with GrovePi and it comes with an instruction book.  I don’t have the book, so I cannot tell you if it’s good or not.)

I’ll blog later about how I used the code for the analog temperature sensor to make the SunFounder analog sensor work and then modified that code to make the Photo Resistor Sensor work with very few changes.  I’ve added some error handling that people on a forum told me about, so the program doesn’t crash anymore.  My ultimate goal is that some of the students at my school are planting an experimental citrus grove and we want to use moisture sensors to automatically trigger an irrigation system.  So, it would have about 40 moisture sensors and a couple of relays to trigger water pumps connected to a tank of reclaimed water.  We might also get a water flow meter to measure how much water we’ve used and track whether the system is working.