Raspberry PI

I2c

Follow instrucctions from: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c , wich is mearly a thing about putting:

/etc/modules::

i2c-bcm2708 i2c-dev

/boot/config.txt::

dtparam=i2c1=on dtparam=i2c_arm=on

Test

Test I2C:

i2cdetect -y 0

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Serial

In order to have RPi serial -> FTDI USB we need TX/Rx and ground. Always check FTDI V setting: RPI is not 5v tolerant.

Disable serial console

RPi default is to bring up serial to the serial console.

/etc/inittab::

#Spawn a getty on Raspberry Pi serial line T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

/boot/cmdline.txt ::

… console=ttyAMA0,115200 kgdboc=ttyAMA0,115200

In order to use the UART for something different than console login these have to be commented and removed, then reboot (or manage inetd).

Serial test

In order to test that the line is available launch a serial connection between two hosts:

conny:  screen /dev/ttyUSB0 115200
rpi:    screen /dev/ttyAMA0 115200

Arduino - RPi serial test

We are going to have first a terminal connection on RPi:

screen /dev/ttyAMA0 9600

Then a sketch running on Arduino (Uno in this case):

byte number = 0;

void setup() {
byte number = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    number = Serial.read();
    Serial.print("character recieved: ");
    Serial.println(number, DEC);
    Serial.flush();
    }
}

Wiring should be like: http://blog.oscarliang.net/ctt/uploads/2013/05/wiring.png with a Logic level converter. Consider that:

  • Rpi can send a Tx -> Arduino with no harm with no leven conversion

  • Arduino need at least a voltage divisior to step down to 3v from 5v.

  • I2C can be done with no conversion if the RPi is muster and there’s no slave pulling up to 5v the line. Seriously: you gotta check that.

Whatever, now let’s make a script on RPI to check the serial connection, first install python serial `` apt-get install python3-serial``

#! /usr/bin/python3

import serial
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
ser.isOpen()

sample = "hello"
ser.write(bytes(sample.encode('ascii')))
try:
    while 1:
        response = ser.readline()
        print(response)
except KeyboardInterrupt:
    ser.close()