Everything comes together for a 1st prototype

This week my 1st self designed PCBs arrived from eurocircuits.com.

Below you see the frontside view, which is holding basically all components, more to that later.
PCB front

This is the PCB backside where actually only the 2X13 PIN connector goes in.
PCB backside

I used some one-way plastic soup bowls to assort the components for the three prototypes.
Assorted components

In the step below all resistors and the two ICs (MCP 3008 and MCP 23S17) have been soldered to the board.
ICs added

Almost there, connector for PIR and IR proximity added, DHT22 (Temperature & Humidity Sensor) and 7-segment displays added.
DHT22 and connector for IR proximity added

Completely assembled it looks like this.
Fully loaded shield prototype

To put it on the RaspberryPi I needed some extra tall stacking headers.
Stacking header für Raspberry Pi (extra tall)

Finally, this is how the shield looks when it is added to the Raspberry Pi.Prototype Shield on Raspberry Pi

IR distance sensor

I want the (still to be added) display only to be powered up when someone is within the range of the device to save some energy. For the implementation of this function I decided to start with an already assembled IR distance sensor from Sharp.

As this sensor is an analog sensor the analog voltage out will range from 3V when an object is only 10 cm away and 0.4V when the object is 80 cm away. Unfortunately the Pi does not have any analog inputs.

My rescue was the great post from Matt @ raspberry-spy.co.uk, where he explains how to measure different light levels on the Raspberry Pi.

So I started from re-building his circuit and run some test measurements, this is how it looked on my breadboard.
LDR with capacitor

For a detailed overview including the Fritzing views please check out Matt’s post. Once I had replicated this set-up successfully I added the IR distance sensor.IR distance sensor on breadboard

With the following code I am able to count how many loops it takes until the capacitor voltage has increased enough to be considered as a HIGH by the GPIO pin (approximately 2V). The number of loops is proportional to the distance of an object in front of the sensor.


use Device::BCM2835;
use strict;

# call set_debug(1) to do a non-destructive test on non-RPi hardware
# Device::BCM2835::set_debug(1);
|| die "Could not init library";

# Variables
my $ir_pin = 24;
my $measurement =0;

# logfile handling
sub logging {

  my $logfile = "/appco.de/log/appco.de.log";

  if ( ! open LOG, ">>", $logfile ) {
    die "Kann Logdatei nicht anlegen: $!";

  my ($sekunden, $minuten, $stunde, $tag, $monat, $jahr) = localtime;
  my $echtes_jahr = $jahr + 1900;
  my $echter_monat = $monat + 1;
  printf LOG "%s.%02s.%02s %02s:%02s:%02s %s\n", $echtes_jahr, $echter_monat, $tag, $stunde, $minuten, $sekunden, $_[0];
  close LOG;


while (1){
  # Discharge capacitor
  # Set GPIO pin to OUTPUT
  Device::BCM2835::gpio_fsel($ir_pin, BCM2835_GPIO_FSEL_OUTP);
  # Set GPIO pin to LOW
  Device::BCM2835::gpio_write($ir_pin, LOW);
  sleep (0.1);

  # Set GPIO pin to INPUT
  Device::BCM2835::gpio_fsel($ir_pin, BCM2835_GPIO_FSEL_INPT);

  $measurement = 0;

  # Count loops until voltage across
  # capacitor reads high on GPIO
  while (Device::BCM2835::gpio_lev($ir_pin) == 0){
    $measurement ++;
    sleep (0.2);
    # stop measuring after 100.000 loops
    if ($measurement > 100000){
      &logging ("canceled");

  &logging ("$measurement");


At the moment the sensor is always powered on, which is probably wasting more energy than I am intending to save by the use of the sensor.

For the future the sensor should only be powered up when the PIR sensor detected a movement and I also want to replace the ootb sensor with a much cheaper solution made out of IR LEDs and an IR receiver.

Posted in Uncategorized | 1 Reply

PIR sensor

As I would like my HomeAutomation to learn my schedule I need to give the Pi some eyes. Let’s start with a PIR (passive infrared) sensor.
The Fritzing screenshot below shows how the sensor is wired up to the cobbler. Basically the red wire goes to 5V0, the black wire to GND and the yellow data wire is hooked up to #25.

PIR Fritzing

As an initial test the following statement executed from the command line served me well

while true; do gpio read 6; done

This reports a 0 when the PIR is not triggered and a 1 when triggered.

As a next step I replicated the very same functionality in a basic perl script using WiringPi-Perl.

#!/usr/local/bin/perl -w</code>

use lib "/appco.de/WiringPi-Perl";

require "wiringpi.pm";

if (wiringpic::wiringPiSetup () == -1)
{ exit 0};

# set pin #6 (marked with #25 on the T-Cobbler) to mode INPUT
wiringpic::pinMode (6, 0);

while (1){
  # read from PIR

  # PIR not triggered
  if (wiringpic::digitalRead (6) == 0){
    print ("off \n");
    wiringpic::delay (500); # milliseconds

  # PIR triggered
  if (wiringpic::digitalRead (6) == 1){
    print ("on \n");
    wiringpic::delay (500); # milliseconds

This code writes “off” to the standard out when the PIR is not triggered and “on” when triggered until you stop the script with ctrl+c.

Raspberry Pi and sensors

Inspired by the Nest® learning thermostat I want to improve the HomeAutomation capabilities of the Raspberry Pi by adding some sensors to enhance the functionality.

Temperature and humidity sensor
To measure the rooms temperature and humidity and to control the electrical valve accordingly. The reason to choose the sensor DHT22 over the DHT11, is the much higher precision. For example for temperature measures: ±0.5°C accuracy (DHT22) vs. ±2°C accuracy (DHT11). To my believe ±2°C is not acceptable for steering the temperature in a room.
Temp/Hum Sensor
Temp/Hum Sensor (on the right hand side) already hooked up to the T-Cobbler on a breadboard

The Pi should be able to notice whether someone is in the room and regulate the temperature accordingly. Over time the server should be able to build a schedule and maintain this schedule. I’ll use this sensor from adafruit.com
PIR Sensor

IR distance / proximity
With this sensor the server should be able to detect a human being close to itself and enable interaction by making the user inteface available. For the time being I will work with the Sharp GP2Y0A21YK0F and may move to a DIY version later on.
IR Distance Sensor