---

Sunday, April 29, 2012

Raspberry Pi and Arduino

Note. There is now a followup to this post here.

The Raspberry Pi is creating quite a storm of interest. I have just got mine and one of the first things that I wanted to try was to get it talking to an Arduino over USB using Python.



.. and you know what? It proved to be a lot easier than I expected. This is mainly because, after all, despite its diminutive price tag, the Pi is just a Linux box. I got communication working both ways, with the Arduino sending 'Hello Pi' to the Pi and at the same time, testing for a digit coming in. When it receives a digit, it flashes the number of times indicated by the digit.

Arduino

Let's start with the Arduino end. I used an Arduino Uno and Arduino software version 1.0. I haven't tried an older board, but I suspect the FTDI generation Arduinos before the Uno may have trouble with USB.

Here is the sketch - paste it into a new Arduino IDE window and load it up onto your Arduino using your regular computer.


const int ledPin = 13;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Hello Pi");
  if (Serial.available())
  {
     flash(Serial.read() - '0');
  }
  delay(1000);
}

void flash(int n)
{
  for (int i = 0; i < n; i++)
  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
}


Raspberry Pi

There is a Python library for serial communications called 'pySerial' which has history with Arduino. So, I stood on the shoulders of giants and adapted the instructions found here.

Step 1. If you are not reading this page on your Pi, then switch now, so you can copy and paste.

Step 2. Browse to here and download pyserial-2.5.tar.gz (106.3 kB) and save it somewhere convenient. I saved it to the 'other' folder on the Desktop.

Step 3. This is a gziped tar file. Which needs unzipping and untaring. To unzip it open a Terminal, which you will find from the 'start menu' under 'accessories'. Now paste the following commands into it.
cd /home/pi/Desktop/other
gunzip pyserial-2.5.tar.gz
tar - xvf pyserial-2.5.tar

Step 4. Install pySerial, by typing these lines in your terminal window:
cd pyserial-2.5
sudo python setup.py install



Step 5. Run Python 2. You will find this from the menu under Programming - Use Python 2 not 3.

Thats it! Now we just need to write some Python to access the Serial port. So type the commands shown in the transcript below.


You type the parts after >>>

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)

Note that the second argument here (9600) is the baud rate and should match whatever you put in your Arduino sketch.


/dev/ttyACM0 is the name for the USB interface to the Uno, at least it was for my Uno. The way to discover the port name is to run the following command in the terminal without the Uno plugged in.

ls /dev/tty*

Then plug in your Arduio and run the command again. If there is a new name, then this is the name of your port.

Now lets start a loop listening for messages from the Arduino.

while 1 :
    ser.readline()

You will need two hit enter twice after you type the second line. Messages should now start to appear!

You can see in the Blue writing where the Arduino is talking to the Pi. Then some error trace as you press ctrl-C to interrupt the messages coming from the Arduino.

When you type

ser.write('5') 

you should see the LED on the Arduino flash 5 times.

There are many possibilities here, we could put a motor shield or LCD shield onto the Arduino and control it from your Pi.


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





                                                                                                                           

Tuesday, April 10, 2012

Steampunk FM Radio - Part 2

In my last post I looked at the finished project.

Update: Monkmakes.com now selling a breakout board for the TEA5767 that simplify things a bit: http://www.monkmakes.com/#!/~/product/category=0&id=36087884


I don't think I would recommend building it the way I describe here. There are a few unsatisfactory things about it. Mainly its difficult to tune - the pot tends to drift a little. Perhaps a vernier dial, or better a rotary encoder. It could also do with a pre-amplifier - the output from the TEA5767 just isn't that high. As a receiver, its also not that sensitive and needs a better antenna than the 2ft of solid core wire dangling out the back that I gave it. Although to be fair I live on the wrong side of a hill for good FM reception,

Anyway, because I don't like to see projects without any details given of how they work, here it is, help your selves and may it inspire you to do it better.

It uses the amp and speaker from a scavenged PC USB powered speaker (right hand side) the TEA5767 module and breakout, described in an earlier post, and a small ATTiny45 board, that has ICSP connector and a 7805 voltage regulator - from another project.

Tuning is accomplished by turning the pot read by an ADC on the ATTiny. This then maps to a frequency that is sent to the TEA5767 by I2C.

The ATTiny is programmed using the Arduino IDE as described here http://hlt.media.mit.edu/?p=1229

It uses the TinyWireM library to the I2C communications. Note you will need to change the header TinyWireM.h to 8MHz instead of 1. See the instructions on the link above.

Here is the script:


#include <TinyWireM.h>

const int potPin = 3;

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;
double frequency;

void setup()
{
  TinyWireM.begin();
  frequency = 97.4; //starting frequency
  setFrequency();
}

void loop()
{
  int reading = analogRead(potPin);
  frequency = map((float)reading, 0.0, 1024.0, 87.5, 108.0);
  frequency = ((double)reading * (108.0 - 87.5)) / 1024.0 + 87.5;
  frequency = ((int)(frequency * 10)) / 10.0;
  setFrequency();
}

void setFrequency()
{
  frequencyB = 4 * (frequency * 1000000 + 225000) / 32768; 
  frequencyH = frequencyB >> 8;
  frequencyL = frequencyB & 0XFF;
  delay(100);
  TinyWireM.beginTransmission(0x60);   //writing TEA5767
  TinyWireM.send(frequencyH);
  TinyWireM.send(frequencyL);
  TinyWireM.send(0xB0);
  TinyWireM.send(0x10);
  TinyWireM.send((byte)0x00);
  TinyWireM.endTransmission();
  delay(100);  
}



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




Sunday, April 8, 2012

Steampunk FM Radio - Part 1

I have been working on a Steampunk FM Radio receiver for a while now. It works and looks pretty good, but there are a few things I would like to improve.



This first post just concentrates on the enclosure. In the next post I will explain the electronics which involve an ATTiny and the subject of my last blog. http://srmonk.blogspot.co.uk/2012/03/tea5767-fm-radio-breakout-board-for.html


Sorry the video is not very good quality. Key features are the illuminated knob, which is made from a copper plumbing fitting that has a hole in the top and is filled with hot-glue-gun glue. There is an LED underneath the knob that lights it up with the glue acting as a diffuser.

The sound does come out of the trumpet like copper thing. This came from an old copper jug that I cut with a rotary tool. This is glued into a hole in the lid, with the speaker mounted directly underneath it.

The box came from eBay and the coil of wire was made by wrapping very small bore copper pipe around a rolling pin.

I consider this a first prototype. Its actually very over engineered internally, and I would like to simplify it, probably by disassembling an off-the-shelf radio.

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