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,, 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:


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.


Raspberry Pi Starter Kit:  $69.95

GrovePi board and sensors:  $75.00

DC Motor:  $5.00

Variety of batteries, clips, and wires

2 Responses to “Sensors and Relays, Raspberry Pi, and Python”

  1. John January 6, 2015 at 8:00 am #

    Great blog post! You brought up some good points about programming languages. One of the things I like about Python is that you can accomplish a lot with just a few lines of code. While it is true that Python is “further away” from the hardware than a language like C or Assembler, you can develop your applications *much* more quickly using Python. In fact it is the availability of so many libraries for Python that make it easier to use. The libraries keep you from having to manage a lot of underlying details and let you focus on what you want your application to actually *do*.

    If you do need more direct control of hardware (usually for things that require very precise timing), you can write that part of your code in C or assembler and then import it into your Python code using extension modules. Python actually plays pretty well with C in this way. By doing this, you can have the hardware control you need and still use Python to quickly develop parts like the user interface.

    One general thing to think about when coding regardless of the language, is to try and do it in a way so that if you look at the code you wrote a year from now, will you be able to quickly and easily understand what it does. You accomplish this by being consistent in naming your variables, keeping the lines neat and organized (Python kind of forces you to do this with the indenting), and putting comments in for code that isn’t “self-explanatory”.

    Something you can do as a learning exercise if you’re up for the challenge, is to create this same project without using the GrovePi and the GrovePi library. You can get readings from the light sensor using an RC (Resistor/Capacitor) timing circuit:

    And then you can control a basic relay using a transistor and a protection diode (also known as a flyback diode or snubber diode – something you should have in your circuit whenever an energized coil is present like in a motor or relay).

    In this case, for the code you would just use the RPi.GPIO library for controlling the GPIO pins directly. (BTW, the relay example I gave you above uses C, but you can do the same thing with Python – I mainly just used that one for the relay circuit itself)

    Anyway, keep up the good work – I always enjoy reading about what you’re working on.


  1. Sensors and Relays, Raspberry Pi, and Python | Raspberry World - January 6, 2015

    […] By krystal92586 […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: