Raspberry Pi Gpio as a ATmega382 (arduino chip) programmer.

In this post, I will outline how to connect a barebone ATmega382 IC (the brains of an arduino UNO) to a raspberry pi using the GPIO pins.

Needed parts:

  • This barebones arduino kit (from iprototype.be) : 7,45 € (includes a socket, a 16 Mhz Crystal, some capacitors, some resistors and a LED (which I accidentaly blew :s)) (build instructions can be found here: http://arduino.cc/en/Main/Standalone. The only difference here is that we don’t need the FTDI-usb-breakout board (which costs 14.95$) (This role is taken over by the raspberry pi). This is powered by a 9V battery.
  • A Logic Level Converter: bought this one from ebay (4,5€). (sparkfun also sells one). You will need one with at least 4 channels. This converts the gpio levels from 5v (arduino) to 3.3 v (raspberry pi), since the pi’s pins aren’t protected and 5v can destroy the pi. Remember to connect the ground of the Pi to the ground of the Arduino (this stopped me in my tracks for a while)
  • I also had an Adafruit Pi T-Cobbler and two breadboards.

Mainly I followed this guide: http://www.deanmao.com/2012/08/06/arduino-and-the-raspberry-pi/

The settings made on the raspberry pi (commenting out all the /dev/ttyAMA0) are important, or it won’t connect serially.

BTW: You can monitor the serial output of the arduino by typing “screen /dev/ttyAMA0”. The only thing is that the upload fails when there is traffic on the serial output of the arduino. It only works after a reset of the arduino.

The other main part is to never ever connect the Raspberry Pi directly to the arduino(-chip), or the Pi can be blown up. That’s where the Logic Level Converter comes in.

The connections to be made (All Trough the Logic Level Converter):

[table]
Arduino, Raspberry Pi

D0 (Rx), Tx
D1 (Tx), Rx
RST, Gpio #24
Vcc, 3.3 V
[/table]


Be Sure to also connect the Ground of the pi directly to the ground of the Arduino, or it won't work!
The RST (reset) to Gpio 24 serves to reset the arduino right before uploading the sketch.
Raspberry pi acting as an arduino sketch uploader to a barebone ATmega382 IC.
Raspberry pi acting as an arduino sketch uploader (to the left) to a barebone ATmega382 IC.(right)

The setup here is an arduino connected to a potentiometer on pin A0 and a LED on pin D9 (PWM).

When you turn the potmeter, the LED goes brighter or less bright, and eventually completely off.

[sourcecode language=”cpp”]</span></span>
<pre><pre>/*
Fade

*/

const int buttonPin = 2
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.flush();
pinMode(buttonPin, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// read the potmeter on A0
int sensorValue = analogRead(A0);

// read the state of the button:
buttonstate = digitalread(buttonPin);

// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
//brightness = half of the potoutput;
brightness = sensorValue/4;

if (buttonState == HIGH) {
Serial.println(brightness);
delay(1000);
}
else {
delay(10);
}
}
[/sourcecode]

Raspberry pi – First experiments

Finally got ‘round to documenting some of my first experiments on the raspberry pi.

i’ve done some temperature logging with an ds18b20 sensor, used a button and lit some leds.

Raspberry pi with ds18b20 sensor, button and some LEDs
Raspberry pi with ds18b20 sensor, button and some LEDs

some python code I used to blink the LED 5 times when i press the button:

[sourcecode language=”python”]
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# button is connected to gpio #23
GPIO.setup(23, GPIO.IN)
# set up GPIO output channel for the LED
GPIO.setup(18, GPIO.OUT)
# blinking function
def blink(pin):
GPIO.output(pin,GPIO.HIGH)
time.sleep(1)
GPIO.output(pin,GPIO.LOW)
time.sleep(1)
return
while True:
if ( GPIO.input(23) == False ):
# blink LED 5 times
for i in range(0,5):
blink(18)
[/sourcecode]

Reading the temperature sensor
[sourcecode]
import os
import glob
import time

os.system(‘modprobe w1-gpio’)
os.system(‘modprobe w1-therm’)

base_dir = ‘/sys/bus/w1/devices/’
device_folder = glob.glob(base_dir + ’10*’)[0]
print device_folder
device_file = device_folder + ‘/w1_slave’

def read_temp_raw():
f = open(device_file, ‘r’)
lines = f.readlines()
f.close()
return lines

def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find(’t=’)
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c

while True:
print read_temp()
time.sleep(1)
[/sourcecode]

The Two Combined: When you press the button, the led blinks five times and each time it probe the sensor for the current temperature:
[sourcecode]
import RPi.GPIO as GPIO
import os
import glob
import time

# blinking function
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)
# set up GPIO output channel
GPIO.setup(18, GPIO.OUT)
os.system(‘modprobe w1-gpio’)
os.system(‘modprobe w1-therm’)

base_dir = ‘/sys/bus/w1/devices/’
device_folder = glob.glob(base_dir + ’10*’)[0]
print device_folder
device_file = device_folder + ‘/w1_slave’

def read_temp_raw():
f = open(device_file, ‘r’)
lines = f.readlines()
f.close()
return lines

def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != ‘YES’:
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find(’t=’)
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c

def blink(pin):
GPIO.output(pin,GPIO.HIGH)
time.sleep(1)
GPIO.output(pin,GPIO.LOW)
time.sleep(1)
return

while True:
if ( GPIO.input(23) == False ):
# blink GPIO17 50 times
for i in range(0,5):
blink(18)
print read_temp()
time.sleep(1)

[/sourcecode]