Day 18 of 30daysOfCreativity - Reading analog values on a PI using an ATTiny45

Nikolaus Gradwohl2013-06-18T20:36:08+00:00

For my Raspberry PI based car-radio that lives in the housing of a blaupunkt radio from the sixties I needed to interface a variable resistor from my pi. Unfortunately the rapsberry pi has no ADC for the GPIO pins so I added a small ATTiny running the arduino-tiny firmware to read the analog values. The ATTiny is programmed as a I2C slave and is accessed in the same way as the radio part of the project.

you can find a tutorial on how to install and run the arduino-tiny firmware here

and you can find the I2C slave library for the attiny chips here

this is my day 18 project for 30daysofcreativity

attiny as a ADC for the Raspberry Pi

This is the pythonscript that runs on the raspberry pi side to read the analog value from the attiny

import smbus
import time
bus = smbus.SMBus(1)

adr = 0x26

def swap( a ):
        return (a & 0xFF) << 0x8 | (a >> 0x8)

def write( chan, d ):
        bus.write_word_data( adr, chan, swap(d))

def read( chan ):
        tmp = bus.read_word_data( adr, chan )
        return swap( tmp )

bus.write_byte( adr, 1 );
a =  bus.read_byte( adr );
b =  bus.read_byte( adr );
print b * 256 + a

and this is the arduino sketch that runs on the ATTiny45

#include "TinyWireS.h" 
#define I2C_ADR 0x26

void setup() {

void loop() {
  byte byteRcvd = 0;
  if (TinyWireS.available()){
    byteRcvd = TinyWireS.receive();
    if ( byteRcvd == 1 ) {
      long a = analogRead(3);
      TinyWireS.send( (byte)(a & 0xFF));
      TinyWireS.send( (byte)(a & 0xFF00 / 256 ));
Tweet This! submit to reddit Digg! Tags: | no comments | no trackbacks

See also:

Day 19 of 30DaysOfCreativity - RGB led for the Raspberry PI based Radio
Day 30 of 30DaysOfCreativity - RadioPI interface module
Day 28 of 30daysofcreativity - testing the radiopi software
Day 27 of 30daysofcreativity - testing the amp module
Day 26 of 30DaysOfCreativity - prototyping the controls



Leave a response

Leave a comment