Friday, February 21, 2014

Raspberry Pi Fridge Minder: receive an email when the door is opened

I decided to see what other kinds of project can be made from the MonkMakes Raspberry Pi Electronics Starter Kit, that I designed.

The kit comes with project cards and a link to download the accompanying Python scripts. And projects already include a light meter and email notifier, so I decided to combine these two projects into one and make something that will send an email whenever the fridge door is opened. 

This is particularly timely as the MonkMakes team are trying to loose some weight at the moment. The paper template on the Raspberry Pi GPIO connector is called a Raspberry Leaf.

It turned out to be pretty straightforward. The breadboard layout is shown below:

BTW. A big thank you to Fritzing for a great design tool.

The photoresistor needs to be inside the fridge, so we can use two of the male to female jumper leads. This will of course also provide us with proof to the eternal question of whether the light really goes off when you close the fridge door.

You can see the completed breadboard and Pi below.

To install the program on the Raspberry Pi, you probably want to connect using SSH (see this Adafruit tutorial). Run the command "nano" and paste the following code into the editor.


import RPi.GPIO as GPIO
import time, math
import smtplib, time



a_pin = 18
b_pin = 23
led_pin = 24

GPIO.setup(led_pin, GPIO.OUT)
GPIO.output(led_pin, False)

def discharge():
    GPIO.setup(a_pin, GPIO.IN)
    GPIO.setup(b_pin, GPIO.OUT)
    GPIO.output(b_pin, False)

def charge_time():
    GPIO.setup(b_pin, GPIO.IN)
    GPIO.setup(a_pin, GPIO.OUT)
    GPIO.output(a_pin, True)
    t1 = time.time()
    t2 = t1
    while GPIO.input(b_pin) == 0:
        t2 = time.time()
        if (t2 - t1) > 1.0:
            return 0
    return (t2 - t1) * 1000000

def analog_read():
    return charge_time()

def send_email(username, password, recipient, subject, text):
    print(username, password, recipient, subject, text)
    smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    smtpserver.login(username, password)
    header = 'To:' + recipient + '\n' + 'From: ' + username
    header = header + '\n' + 'Subject:' + subject + '\n'
    msg = header + '\n' + text + ' \n\n'
    smtpserver.sendmail(username, recipient, msg)

username = raw_input("Sending gmail address? ")
password = raw_input("Sending gmail password? ")
recipient = raw_input("Send email to? ")
message = "Someone just opened the fridge door!"

while True:
    reading = analog_read()
    if reading > 0: #dark so timeout
        GPIO.output(led_pin, True)
        send_email(username, password, recipient, subject, message)
        GPIO.output(led_pin, False)

You can test out the program without installing it on your fridge. Start the program using the command:

sudo python

There may be a few warnings about GPIO channels being in use, which you can ignore. Cover the light sensor with a cloth or place it somewhere where it will be completely dark.

You will be prompted for your gmail email address, password and who you want to be sent an email when the fridge door is opened.

$ sudo python 
Sending gmail address?
Sending gmail password? jkshdgfkayig
Send email to?

When its running, you can uncover the sensor and you should see some trace that the email has been sent. Similarly, any errors will appear. 

The light sensor having fairly thin wires attached can just be shut in the door.

Friday, January 31, 2014

BeagleBone Black Collars

Almost a year ago, I posted a blog entry on the Raspberry Leaf. A template to tell you which pins are which on the GPIO header. This was hugely popular and many people downloaded the PDF and printed their own leaves. Since then MonkMakes has been selling large numbers of ready-made and drilled leaves for the Pi.

I have just finished a book on the BeagleBone Black and of course suffered the same problem of working out which pin is which. Even worse on the BBB because it has so many GPIO pins.

So....... drum roll please ....... Enter the BeagleBone Collar!

These have self-adhesive strips on the sides and fit onto the edges of P8 and P9 like ears (another candidate name).

Here is the PDF.

But if you want a ready-made one with self-adhesive tabs, visit

Learning to Solder? Try this Mega Kit from Jack Christensen.

Jack Christensen sent me one of his One Million Ohm's kits, so last weekend, my son and I decided to put it together. Well he made it and I got in the way and 'hovered' like a typical dad.

WARNING: When constructing this project, always remember that Ohm's Law dictates that V = IR, so if you can persuade just 100mA to flow through it, there will be 100,000 Volts generated!

Just kidding. The big resistor in the middle, serves no more purpose than being the butt of the joke. It is surrounded by four LEDs that flash in various patterns controlled by two buttons. The whole thing runs from an ATTiny and includes pads for attaching ICSP headers if you feel like hacking it.

Its really easy to put together, and a great first soldering project.

It includes everything you need, except a pair of AA batteries, and my son very much enjoyed putting it together. The only things to watch out for are putting the LEDs and chip the right way around.

Some of my books, that may interest you:

Monday, January 27, 2014

Conductive Paint Experiments

I have been experimenting with some Bare Conductive Paint that I bought from CPC after seeing it being used in screen printing at Lancaster's Highwire Lab. The paint is made by Bare Conductive in the UK.

The thirst thing I wanted to know was just how conductive is this stuff. So I cut myself a stencil with tracks of 1mm (40 mils) and 3mm (120 mils) and daubed paint onto the paper below.

It leaked a bit. I am really no good at art. However, the final tracks are good enough for a rough test.

Being somewhat impatient, I first tried measuring the resistance before the tracks were properly dry. The resistance was actually several hundred kΩ but falling as I watched. There is clearly a very high resistance until the paint is properly dry, which took perhaps 30 mins.

I left it for another hour to be sure and these are the results:

The 1mm (40 mils) track had a resistance of 780Ω / cm (2kΩ / inch)
The 3mm (120 mils) track had a resistance of 340Ω / cm (850Ω / inch)

So, its not quite like painting with wire at all, but that's not necessarily a bad thing as it means that you could very easily paint yourself some resistors. If you are using LEDs then just the tracks are going to provide more than enough current limiting resistance.

My next experiment was to see how well a surface mount 1206 LED would stick to the paint. So, I painted the arrangement shown below and popped the surface mount LED onto the wet paint with a pair of tweezers.

It actually sticks really well and shows no sign of falling off even when the paper is shaken vigorously and even curled up.

So the next step is to make an Origami frog and put a pair of SMT LEDs in series on its head to make it look demonic. The connections are made by painting back towards the frog's legs, where croc clips connected to a battery could be attached.


This paint is an excellent product and very easy to paint with.

I will be trying out some proper stencilling with PCB layout's from EAGLE. It will be fun planning things out to account for the high resistance of the wire.

I also think there is probably some fun to be had with pre-painted Origami.

Tuesday, January 21, 2014

MCP79412 Real Time Clock Breakout Board Review

Recently, Jack Christensen, sent me a few goodies, including this really neat and low cost Real Time Clock (RTC) module. A RTC is useful, because it will keep track of the time even when the power is removed from your Arduino. It uses a long life lithium battery.

It comes assembled apart from the header pins which are included and just need soldering. You only really need to solder header pins to the I2C side of the board. Its a nice design, the pin headers holes just the right size so the pins don't fall out when soldering.

The board costs just $10 (on special offer at the time of writing) from Tindie and comes in an anti-static bag with three URLs on it:
  • Arduino Library:
  • Blog Post:
  • Tindie Store:

I downloaded the library from the URL on the bag fetching the zip file, copying it to my Arduino/libraries folder and renaming it to remove the word "master" from the end of it. I then restarted the Arduino IDE and then run the examples program TimeRTC.

The module is easily connected to an Arduino using four male to female jumper leads.

The connections are:
  • Vcc on the RTC to 5V on the Arduino
  • GND on the RTC to GND on the Arduino
  • SDA on the RTC to SDA on the Arduino
  • SCL on the RTC to SCL on the Arduino

Uploading and running the test program produced the following output in the Serial Monitor.


This module does not use the usual DS1307 chip, but rather the Microchip MCP79412. This can be calibrated and is more accurate than the DS1307. You can read more about it in Jack's blog:

My recommendation is that if you need an accurate low cost RTC then you will not go wrong with this excellent device.

About the Author
These are my books. Click on the image below to find out more about them.

Saturday, January 18, 2014

Raspberry Pi Electronics Starter Ki

One of the most frequently asked questions of me is 'where can I buy the parts for X' where X is one of the project in my books.

In an attempt to remedy this situation, my partner Linda and I have started on little on-line store ( to make and sell things related to by books. That will include the books themselves, signed by the author if you wish (this is very good for my ego) but also kits of parts.

The web store is based in the UK, but we have no problems with shipping world-wide. Its very early days and we are still finding our way. So if you have any suggestions on what else we should sell, please contact

Our first product is an electronics starter kit for the Raspberry Pi. 

This is breadboard-based and although it has the parts needed to build Recipes 9.1, 9.2, 9.3, 9.9, 9.12, 11.1, 11.2, 12.2 in my Raspberry Pi cookbook, it also includes 10 project cards that give you the breadboard layouts for ten projects:

  • Make an LED blink
  • Make 2 LEDs blink
  • RGB Colour Display
  • Thermometer
  • Thermometer Plus
  • Reaction Timer
  • Light Meter
  • Light Harp
  • Proximity Detector
  • Email Notifier
The projects are also accompanied by a download of Python programs to support the projects and provide user interfaces.

The kit includes the following components:

  • 470Ω resistor
  • 1kΩ resistor (1)
  • 4.7MΩ resistor (1)
  • Red LED (2)
  • RGB LED (1)
  • Switch (2)
  • 330NF capacitor (1)
  • thermistor (1)
  • photoresistor (1)
  • buzzer (1)
  • raspberry leaf (1)
  • breadboard (1)
  • male to male jumper leads (10)
  • male to female jumper leads (10)
  • 10 easy to use instruction cards
The Raspberry leaf (which we also sell separately) is a nicely printed template that has holes where the GPIO pins fit, making it easier to know which pin is which. I originally blogged about this back in February last year and it was a huge hit. You can of course just download and print the PDFs, but if you want something ready made, then we sell them ready made.

Anyway, please take a look at our store and let us know what else you would like to see us selling.

About the Author
These are my books. Click on the image below to find out more about them.

Tuesday, December 24, 2013

Analog Sensors without Analog Inputs on the Raspberry Pi

The Raspberry Pi does not have any analog inputs, but that does not mean that you can't use some types of analog sensors. Using a couple of resistors and a capacitor, you can use a "step response" method to measure resistance. Which is just great if you are using a pot, photoresistor or thermistor.
The Recipe that follows is taken from my new book "The Raspberry Pi Cookbook". This way of using sensors was inspired by this work from Adafruit.

To make this recipe, you will need:
• Breadboard and jumper wires 
• 10kΩ trimpot 
• Two 1kΩ resistors 
• 220 nF capacitor 

Open an editor (nano or IDLE) and paste in the following code. As with all the program
examples in this book, you can also download the program from the Code section of
the Raspberry Pi Cookbook website, where it is called

import RPi.GPIO as GPIO
import time

a_pin = 18
b_pin = 23

def discharge():
  GPIO.setup(a_pin, GPIO.IN)
  GPIO.setup(b_pin, GPIO.OUT)
  GPIO.output(b_pin, False)

def charge_time():
  GPIO.setup(b_pin, GPIO.IN)
  GPIO.setup(a_pin, GPIO.OUT)
  count = 0
  GPIO.output(a_pin, True)
  while not GPIO.input(b_pin):
  count = count + 1
  return count

def analog_read():
  return charge_time()
  while True:

When you run the program, you should see some output like this:

$ sudo python

The reading will vary between about 10 and about 170 as you rotate the knob of the

To explain how this program works, I first need to explain how the step response technique
can be used to measure the resistance of the variable resistor.

This way of doing things is called step response because it works by seeing how the circuit
responds from the step change when an output is switched from low to high.

You can think of a capacitor as a tank of electricity, and as it fills with charge, the voltage
across it increases. You can’t measure that voltage directly, because the Raspberry Pi
doesn’t have an ADC converter. However, you can time how long it takes for the capacitor
to fill with charge to the extent that it gets above the 1.65V or so that constitutes
a high digital input. The speed at which the capacitor fills with charge depends on the
value of the variable resistor (Rt). The lower the resistance, the faster the capacitor fills
with charge and the voltage rises.

To be able to get a good reading, you must also be able to empty the capacitor each time
before you take a reading. Connection A is used to charge the capacitor through Rc and Rt, and connection B is used to discharge (empty) the capacitor through
Rd. The resistors Rc and Rd are used to make sure that there is no way too much current
can flow as the capacitor is charged and discharged.

The steps involved in taking a reading are first to discharge the capacitor through Rd
and then to let it charge through Rc and Rt.

To discharge it, connection A (GPIO 18) is set to be an input, effectively disconnecting
Rc and Rt from the circuit. Connection B (GPIO 23) is then set to be an output and low.
This is held there for 5 milliseconds to empty the capacitor.

Now that the capacitor is empty, you can start to allow charge to flow into it by setting
connection B to be an input (effectively disconnecting it) and then enabling connection
A to be a high output at 3.3V. Capacitor C will now begin to charge through Rc and Rt.
The while loop will then simply count as fast as possible until the voltage at connection
B switches from being low to high at about 1.65V. At that point, the counting stops and the count value is returned.

The figure below shows how a resistor and capacitor in this kind of arrangement charge and
discharge as the voltage is toggled between high and low.

You can see that the voltage at the capacitor increases rapidly at first but then tails off
as the capacitor becomes full. Fortunately, you are interested in the area of the curve up
until the capacitor reaches about 1.65V, which is a fairly straight line, meaning that the
time taken for the voltage across the capacitor to rise to this point is roughly proportional
to the resistance of Rt and hence the position of the knob.

This approach is not hugely accurate, but it is very low cost and easy to use.

About the Author
These are my books. Click on the image below to find out more about them.