Saturday, April 13, 2013

Serial (UART) Communication between a Netduino Plus 2 & a Raspberry Pi

This code is part of a small experiment to work out how to send data between a Raspberry Pi and a Netduino Plus 2 via a serial (UART) connection. See this posting on one of my Arduino/Netduino blog for more information (including notes on how the two devices are wired together).

This code waits for a '@', '$', or '#' sent as a 1-byte "command" from the Netduion. The Raspberry Pi then responds with the requested information. The end of the response is marked with a linefeed (0x0A) character.

Python Code

import serial
import datetime

ser = serial.Serial("/dev/ttyAMA0", baudrate=115200)
while True:
        # Read 1-byte command from Netduino
        c =
        # @ = get full date & time
        if c == '@':
                now =
        # $ = get date
        elif c == '$':
                today =
        # # = get time of day
        elif c == '#':
                now =
                time = "%02d:%02d:%02d" % (now.hour, now.minute, now.second)

Saturday, April 6, 2013

Text to Speech with a EMIC2 & a Raspberry Pi

The EMIC2 text to speech module does not require any special libraries and it communicates with the controller using a serial connection, so it is not difficult to get it working with a Raspberry Pi. Below is an example that uses Python to send messages to the EMIC2.  

The EMIC2 is a 5V device, so a logic level converter is required on the serial connection to and from the Raspberry Pi.

For audio output, I have used a cheap thin speaker from Sparkfun, but you can also use the headphone jack on the module.

The RXD and TXD pins on the Pi connect to the Linux serial device ttyAMA0. By default, though, this device is used for console output. Follow the 3 steps on the Adafruit Learning System page, "Freeing UART on the Pi."


EMIC2  Logic Level Converter  Raspberry Pi
GND    HV GND     ----->      GND
5V     HV HV      ----->      5V
SIN    HV TXO     LV TXI      TXD
                  LV LV       3V3
                  LV GND      GND

Connect SP- & SP+ to the speaker.


Python Code

The following demonstration code counts out load. Depending on your actual output, ou may need to experiment with the time.sleep() calls to get the values right.

import serial
import time

serial = serial.Serial("/dev/ttyAMA0", baudrate=9600)
serial.write("V15\n") # Adjust volume
c = 0
while True:
        data =
        if data == ':':
                buffer = "S%d" % (c)
                c = c + 1


For documentation on the various EMIC2 commands, see the manual.

Wednesday, April 3, 2013

Reading Time & Date from a Chronodot Using Python with a Raspberry Pi

Here is a quick example of how to read the time and date from a Chronodot using the Python SMBus module (for I2C communication).


Chronodot   Raspberry Pi
GND         GND
VCC         3V3
SCL         SCL0
SDA         SDA0

The Raspberry Pi has built-in pull-up resistors on the SCL and SDA lines, so external pull-up resistors are not needed.


import smbus
import time
import time
bus = smbus.SMBus(1)
address = 0x68
data = bus.read_i2c_block_data(address, 0)
ss = (data[0]/16*10) + (data[0]%16)
mm = (data[1]/16*10) + (data[1]%16)
hr = (data[2]/16*10) + (data[2]%16)
day = (data[3]/16*10) + (data[3]%16)
date = (data[4]/16*10) + (data[4]%16)
month = (data[5]/16*10) + (data[5]%16)
year = (data[6]/16*10) + (data[6]%16)
buffer = "%02d:%02d:%02d %02d/%02d/%02d" % (hr, mm, ss, month, day, year)
print buffer