Sunday, 23 September 2012

In this tutorial I will be detailing how to generate analogue voltages using a Raspberry Pi and an external Digital to Analogue Converter (DAC) to control the brightness of an LED.Although the Raspberry Pi can interface with various low-level hardware using it's GPIO lines, SPI, I2C and serial UART interface, it does not have an DAC interface for generating analogue voltage levels. Instead the user must use an external DAC device controlled by one of the available low-level hardware interfaces, such as SPI and I2C.I will be using the MCP4725 I2C DAC Breakout board as described here to control the brightness of the LED.

(Please note that I have no affiliation with Proto-Pic other than being a regular customer.)

MCP4725 I2C DAC Breakout Board

The datasheet for the MCP4725 can be found on Microchip's website here. Suffice it to say that the DAC has a single analog output that can drive up to 25mAmps. This will be sufficient to power our LED.

To configure the voltage output that appears on the ANALOGUE pin, the user must send the appropriate '12 bit DAC Input Data' value to the DAC over the I2C bus. The user can also set this value into the device's EEPROM (permanent storage) so that a particular output voltage will be automatically set when the DAC is powered on, otherwise the DAC defaults to 0Volts output at power on.The following gives the equation for input data value/code and output voltage, in our case Vdd will be 3.3volts:

Input Code and Output Voltage

Configuring the MCP4725 DAC is slightly different to the likes of the TMP102 I2C device as it does not have a register (data address) that you specify.Instead, you simply write the configuration to the device without specifying a register number.We will be setting the output voltage using the 'Fast Mode' configuration, which means when the DAC is power cycled our value will be lost.This 'Fast Mode' I2C command takes the following structure:

1st Byte (Standard I2C Device Addressing with write)

2nd Byte

3rd Bye

0x60

0x0Y

Where Y is the most significant 4 bits of our output voltage setting.

0xZZ

Where ZZ is the lower byte of our output voltage setting.

CircuitSo, we are going to connect our I2C device to the appropriate pins on the Pi's GPIO header:

Raspberry Pi GPIO Header

MCP4725 Breakout board

Description

Pin1/3V3

Vcc

This will provide power and Vref to the MCP4725 device.

Pin6/Ground

GND

Device power rail ground.

Pin3/SDA

SDA

Data line for the Pi's I2C interface 0.

Pin5/SCL

SCL

Clock line for the Pi's I2C interface 0

We then need to connect the LED to the ANALOGUE pin on the breakout board and put the 200 ohm resistor in series between the LED and ground.Enabling I2C on your Raspberry PiPlease follow my instructions here to enable I2C on your Raspberry Pi.Verify you can see the DAC on bus zero using the i2cdetect command:

Here we can see our device at address 0x60.Controlling the LED BrightnessWe will use the linux i2cset command to configure the DAC output voltage and therefore the brightness of the LED:Full brightness (3.3volts, 0xFFF input code):pi@raspberrypi ~ $ i2cset -y 0 0x60 0x0f 0xff bLow Brightness (1.86volts, 0x900 input code):pi@raspberrypi ~ $ i2cset -y 0 0x60 0x09 0x00 bNote we are not really using i2cset as it was designed. Looking at the man page for i2cset, the command takes the following basic form:i2cset -y i2cbus chip-address data-address [value] ... [mode]We are using the data-address (register) field as the 2nd byte in the command and our third byte is the first [value] in the command. A little bit confusing, but important to be aware of.ConclusionSo, in this tutorial we have shown how to setup and control the brightness of an LED using the Raspberry Pi's I2C bus and an external I2C DAC.

Thursday, 20 September 2012

In this tutorial I will detail how to interface to an I2C device using your Raspberry Pi. Specifically we will be reading the temperature from an I2C based TMP102 breakout board. You won't need any programming skills to do this, just the ability to connect up the simple circuit to your Pi and be able to use the command line. It will help to have some understanding on how I2C buses work.
Disclaimer: I take absolutely no responsibility if you blow up your Raspberry Pi or anything else while following this tutorial.

An I2C based TMP102 digital sensor breakout board (example here). You will need to solder on some break away headers onto the breakout board so you can mount it on your breadboard.

(Please note, I have no affiliation to Proto-Pic other than being a regular customer.)TMP102 Breakout BoardThe TMP102 breakout board I am using is this one from SparkFun. This device can measure a temperature range of -40°C to +125°C, with a resolution of 0.0625°C and an accuracy of 0.5°C.

The TMP102 has 4 registers that are accessible through the I2C bus:

Register 0 Temperature Register (Read Only)

Register1 Configuration Register (Read/Write)

Register 2 TLOW Register (Read/Write)

Register 3 THIGH Register (Read/Write)

We are only interested in Register 0, for further info on the other registers, the datasheet for the TMP102 device can be found here. Two bytes can be read from register 0:

Byte 1: Full degrees

Byte 2: Fraction degrees

CircuitSo, we are going to connect our I2C device to the appropriate pins on the Pi's GPIO header:

Raspberry Pi GPIO Header

TMP102 Breakout board

Description

Pin1/3V3

V+

This will provide power to the TMP102 device.

Pin6/Ground

GND and ADO

Device power rail ground.

Pin3/SDA

SDA

Data line for the Pi's I2C interface 0.

Pin5/SCL

SCL

Clock line for the Pi's I2C interface 0

Note that AD0 in the TMP102 breakout board is used to define the least significant bit of the I2C address of the TMP102 device. If it is connected to ground, the address of the device is 0x48, if connected to 3v3 the address is 0x49. This allows up to two TMP102 devices to exist on the same I2C bus. In my case I have connected it to ground.

Enabling I2C and Installing I2C ToolsAssuming you have not enabled I2C on your Raspberry Pi before:

Enable I2C in modprobe: Have a look at the /etc/modprobe.d/raspi-blacklist.conf file, comment out the two lines:

The '-y' option disables interactive mode for the command and the '0' is the I2C bus to scan. You can also run this command on the Pi's second I2C bus by specifying '1' instead.We can see that it has found our TMP102 device at address 0x48.

To read the temperature from our temperature sensor, we use the i2cget command to read a single byte (Byte 1 - full degrees) from the temperature register (0x00) of the device.pi@raspberrypi ~ $ i2cget -y 0 0x48 0x00 b
0x16
Converting this hexadecimal value to decimal, we get our temperature of 22°C.If you want a more precision on the temperature, you can read both the full and fractional bytes from register 0 as follows:pi@raspberrypi ~ $ i2cget -y 0 0x48 0x00 w0xa015

This gives us byte 2 (0xa0) and byte 1 (0x15), but as a 16bit hexadecimal number and in the wrong order. To convert to °C, swap around the bytes, shift right by 4, convert to decimal and multiply by 0.0625.E.g.

dec(0x15a0>>4) * 0.0625 = 21.625°C

ConclusionSo, we have been able to set up the circuit to interface with the TMP102 temperature sensor, enabled I2C on the Raspberry Pi, install the necessary I2C tools and queried the temperature from the sensor.As a further exercise, the reader can experiment with reading and writing (using the i2cput command) to the other registers available in the TMP102.

Saturday, 30 June 2012

I use my Beagleboard as a very simple home server, mainly for development and managing my subversion repository. I keep the board on the carpet, beside my router and decided I needed to get my hands on a case to protect it. After looking around the net I couldn't find anything that wasn't ridiculously expensive, so decided to sort something myself.

Basic Beagleboard Case

As you can see from the picture, it is very basic, but fits the purpose. It consists of two custom cut pieces of thin plywood, some spacers and screws and took less than twenty minutes to cut and assemble. Plus it cost less than a fiver.

Sunday, 29 April 2012

Welcome to part 3 of the ProBee Based Home ZigBee Network series. In this entry I will be looking at how to display the temperature reported from our ProBee based sensor node onto a webpage using a custom Python script and Open Energy Monitor's emoncms software. "﻿Emoncms is a powerful open-source web-app for processing, logging and visualising energy, temperature and other environmental data."Will will need to have a working sensor node network comprising of a co-ordinator and at least one sensor node, so make sure you have followed the part 2 of this series!Additional things you will need:

A Web Server: A Linux based PC/server with Apache, MySQL Server and Python installed. I'll be using my Beagleboard running Ubuntu Server, but any major Linux distribution should be suitable (including running one from a virtual machine assuming your virtual machine has access to the ZigBee coordinator's USB serial interface).

Emoncms : The latest version of emoncms installed on the server. You can get it here (at time of writing version 3 is that latest). Installing it is quite trivial once you have your Linux server setup, just follow the installation guide here.

This tutorial should work just as well with a Windows based server, but I will leave it up to the reader to investigate.

Flow of temperature data

So we need to do the following:

Verify sensor node and network - Check what serial device our coordinator is connected to and verify we are receiving temperature data from the sensor node through the co-ordinator's serial interface.

Verify
emoncms Installation - Check that emoncms is working correctly and receiving sample commands from the browser.

Create the serial data parsing script - Write a Python script to parse the serial data outputted by the ProBee coordinator and send this data to emoncms for storage and display. The script is very basic, so if you don't know Python you could easily port it to your preferred language.

Integration - Integrate it all so our temperature data is displayed on our web server.

Run at start-up (optional) - I will show you how to get the serial data parsing script running automatically when your Ubuntu server is powered on.

Verify Sensor Node and NetworkPlug your pre-configured ProBee coordinator (ProBee USB dongle) into your server and power on your ProBee sensor node (you should have followed part 2 to get these ready). We now need to connect to the ProBee USB dongle's USB serial port using a terminal application.To find the device name for your USB dongle, use the dmesg command:

The Probee dongle uses an FTDI USB to serial interface chip, which in my case is attached to ttyUSB0.Now use minicom (Linux equivalent of hyperterminal) to view the serial data (containing our sensor node temperature) coming from the co-ordinator. If you haven't use minicom, there is a good tutorial here.You should get output similar to this:

Verify Emoncms InstallationHtml post requests are used to send input data to the emoncms server. Our script will be creating requests to send the temperature data to the server each time we receive new data from our temperature sensor.Now to verify the emoncms installation: Browse to your emoncms installation (in my case http://mylocalserver/emoncms3/user/view), and click on the Account tab. Then click on the "try me" link which will send a sample html post request to the server. Then click on the Inputs tab, and you should see two new feeds in there that were updated several seconds ago.

Emoncms validation

Create Serial Data Parsing ScriptThe script will have to do the following:

Open the serial port to the ProBee dongle.

Wait for and read a line of data from the serial port.

Parse that line and generate the html post request to the emoncms server.

IntegrationWe are now ready to test the system. Open a browser and browse to your emoncms installation and click on the Inputs tab. Plug in your ZigBee co-ordinator (ProBee dongle), run your serial data parsing script and power on your sensor node.You should now see the temperature input updating every 5 seconds on the Inputs tab. You can now add widgets to your dashboard and display various graphs, etc of this data.

Run at Start-Up

As the root user, save the following script to /etc/init.d/home_monitoring.sh, not forgetting to add in the full path to your script.

Make the script executable: by running the command: "chmod 755 /etc/init.d/home_monitoring.sh".Now register the script so it will be called at startup/shutdown: "update-rc.d home_monitoring.shdefaults".You can test your script runs at startup by restarting your server, your emoncms dashboard should be continuously updated by your sensor node.

ConclusionYou should now have a basic home temperature monitoring system using ProBee based Sensor Node and Co-ordinator and emoncms.

Sunday, 15 April 2012

OverviewWelcome to part 2 of the ProbBee based home Zigbee network. In this part of the series I will be detailing how to setup, configure and test a temperature reporting end-node. We will follow these steps:

Design and build the circuit.

Configure the ProBee module to periodically report the temperature and enter a low-power sleep mode.

The ProBee manager software, to configure the end-node and co-ordinator.

A ProBee expansion board, which will allow us to mount the module onto a breadboard. Please contact me if you need an expansion board as I have some spare ones.

A TMP35 temperature sensor in DIP package, used for sensing the room temperature. These are extremely easy to use, for more info please look at ladyada's great overview here.

A DIP LED used for indicating the status of the ProBee module.

A power circuit for regulating our battery power supply to the 3.3 volts required by the ProBee module and TMP36. Alternatively, the easiest thing to do would be to get a Breadboard Power Supply like this from Sparkfun: Breadboard Power Supply 5V/3.3V, it won't be the most efficient approach, but will be fine for what we need.

You can see the main components: the ProBee module, TMP36 temperature sensor and status LED. For an excellent tutorial on the TMP36, please see LadyAda's page here. Note that I have connected the output of the temperature sensor to PB_5/ADC0 pin of the ProBee module.

My ZigBee Temperature Monitoring Node

We can see from the above picture I have chosen to use a regulator circuit on the board. If you are very new to electronics I'd suggest to make life easier and purchase the previously mentioned Breadboard Power Supply 5V/3.3V.Also from the picture above you can see that I have the ProBee module pugged into an expansion board so the board can be plugged into a breadboard. I have several of these boards spare and will happily post them to any reader for only the cost of postage.

Configuring The ProBee ModulesFirst of all make sure your ProBees are using firmware v1.5 or higher.Secondly, I'm not going to delve too deeply into the config of the sensor node and co-ordinator. Programming the ProBee modules is very straight forward, Sena have some good documentation on their website here. I would highly recommend reading the ZigBee Network Configuration section of the ProBee-ZE20S User Guide!I have created two configuration files that can be programmed into the ProBee modules using Sena's ProBeeManager application:

Our Zigbee temperature sensor node. This configures the ProBee module to sleep for 5 seconds, wake up, take samples of it's digital/analog I/O ports, transmit this to the co-ordinator and go back to sleep (see section3.4Setting up ZE20S as a Sleepy End-Device of the user manual): Probee_SED_Blog_150412_115200baud

Note the serial interface baud rate for both configurations is set to 115200 baud.TestingConnect the Co-ordinator (ProBee usb dongle) into a usb port on your computer and open a serial connection to it using your favourite terminal application. If you are using my config files above, ensure you have the baud rate set to 115200.Now power on your sensor node. You will notice that the status led flashes on briefly every 5 seconds. This is the ProBee waking from sleep node and transmitting the sample of the analog and digital IO.Looking at the Co-ordinator's terminal output, you can see the messages coming from the sensor node:

Messages received by the co-ordinator from the sensor node

Each line represents a single message received by the co-ordinator from the sensor node. We are interested in the field reading 1C3F, this is the hexadecimal value for the Analog sample of ADC0 port (which the TMP36 is connected to) on our sensor node.Decoding the TemperatureThere are two steps to the decoding of the actual temperature. First we need to get the measured voltage (TMP36 output voltage):

Vadc = AdcValue * 0.1

Then to get the temperature:

Temperature (Celsius): (Vadc - 500) * 0.1

Therefore:

Vadc = 0x1c3f * 0.1 = 723 mV

Temperature = (723 - 500) * 0.1 = 22 Deg Celsius

Note that the ProBee devices can read in a voltage range of 0 to 800mV!NextSo, we now have a working ZigBee temperature sensor node! Next up is to do something with the raw text that is outputted by the co-ordinator over the serial interface. In Part 3 we will be using emoncms to display the temperature data on a web page.

Saturday, 7 January 2012

OverviewIn this blog series I will be showing you how to create a ZigBee based home network for monitoring room temperature using Sena's ProBee Modules and OpenEneryMonitor's emoncms for recording and displaying the temperature data.Specifically, I'll be using the ZE20S modules with chip antenna for the sensor nodes and a ProBee-ZU10 usb dongle that will act as the network co-ordinator and feed temperature data into the monitoring computer.The following are assumptions on the readers knowledge/ability:

Basic understanding of ZigBee networks, for more info please see here and here.

Basic breadboard skills.

Access to a Linux based server for running the emoncms software.

ProBee ZigBee ModulesThe main thing I like about these ProBee modules is that they are cheaper than the corresponding XBee modules, and pretty much provide the same functionality (the ProBee modules actually have more digital and analog I/O pins that the XBee).The cheapest ZigBee compliant ProBee module (ZE20SSC-00 with chip antenna) costs about US$17. The cheapest ZigBee compliant XBee module (Series 2 XBee ZB with chip antenna) retails for around US$25! Interestingly both modules have an underlying Ember ZigBee chip with EmberZNet, Ember’s ZigBee compliant embedded mesh networking software.

Sena's ProBee Modules

System OverviewThe following diagram outlines the components of the system:

Overview of Network Components

ZigBee Temperature Sensor End Devices - Each end node will comprise of a ProBee ZE20S module, with temperature sensor, power supply circuit and batteries. As these nodes are battery powered, they will be configured to enter battery saving sleep mode and wake up periodically to report the temperature reading.

ZigBee Router - This will route the messages containing the temperature readings from the end devices to the ZigBee coordinator. The router is optional and only necessary if you don't have enough range between your end devices and Coordinator. It will consist of a ZE20S module and power supply. The router must remain powered on all of the time and as such will be mains powered.

ZigBee Coordinator - This will receive our temperature readings from the End Nodes in the network and display/store the measurements using the emoncms software. It will comprise of a ProBee USB dongle and, in my case, a Beagleboard. A PC or MAC could easily be used instead of the Beagleboard, as long as it runs emoncms.

Building The System

The following entries cover the steps that are required to complete our system: