วันอังคารที่ 30 เมษายน พ.ศ. 2556

1 GPIO Input


.1 GPIO Input

GPIO peripherals vary quite widely. In some cases, they are very simple, a group of pins that can be switched as a group to either input or output. The input and output voltages are typically, though not universally limited to the supply voltage of the device with the GPIOs on, and may be damaged by greater voltages.
Some GPIOs have 5 V tolerant inputs: even on low supply voltages, the device can accept 5V without damage.
For Raspberry Pi, we present an examples of how to adapt the voltage level of a 5V sensor measure to prevent possible damage. 
Components for this examples and voltage adaptation circuit can be founded in the Starter Kit for Raspberry Pi (A/V Edition) 
When a GPIO pin is set as an input with a basic push button example, we can have these voltages incompatibility problems. 
 This circuit is wrong because when you press the button the GPIO input is connected to 5 volts, so our device may be damaged.
 However, this can be avoided by simply using a resistor in the push-button cable. The value of the resistor is determined by the leakage current of the GPIO pin (the current used by the circuit to read the pin) and the amount of voltage drop it creates as a result. With the 5K resistor we obtain 3.3V in the GPIO input.
Vgpio = 5V·(10K/(10K+5K)) = 3.3V

Raspberry Pi and Arduino via GPIO UART


Raspberry Pi and Arduino via GPIO UART

In an attempt to get my Raspberry Pi talking to my Arduino I’m exploring various different options. The first was to just use the USB connection, but that was too simple. So, here is how to connect the two using the UART on the GPIO pins of the Raspberry Pi.
To make testing easier I wanted to keep the Arduino’s serial connected via USB to my PC so I can print messages there and read it with the Serial Monitor. This meant using theSoftSerial library to implement a second serial port to talk to the Raspberry Pi. To protect my Raspberry Pi and to convert the 5V of the Arduino to 3.3V the Raspberry Pi needs I used a CD4050.
To show how this works the Arduino is running a small program that reads from the Raspberry Pi’s and copies this to my PC via USB.
By default the Raspberry Pi uses the UART in two ways:
  1. Console Messages (including bootup messages)
  2. A getty so you can login via serial
To use this serial port for your own uses you need to disable these two services. I decided to leave them enabled to test how well the serial connection works. However, the SoftSerial library on the Arduino doesn’t work so well at the default baudrate of 115200, so I changed this on the Raspberry Pi to 9600:
To change the console baudrate, edit /boot/cmdline.txt to look like this (this is all one line):
dwc_otg.lpm_enable=0 console=ttyAMA0,9600 kgdboc=ttyAMA0,9600 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
Also, edit /etc/inittab to change the baudrate of the getty (you should fine a line like this with the baudrate of 115200, change that number to 9600):
2:23:respawn:/sbin/getty -L ttyAMA0 9600 vt100
Ok, now upload the program to your Arduino:

/*

 Connects Arduino to Raspberry Pi
 Arduino: SoftSerial
 Raspberry Pi: GPIO UART

 This is just a simple passthrough, based on Arduino SoftSerial example

 */
#include 

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()  
{
 // Open serial communications to PC and wait for port to open:
  Serial.begin(57600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println("Connected to PC");

  // set the data rate for the SoftwareSerial port to Raspberry Pi
  mySerial.begin(9600);
}

void loop() // run over and over
{
  // If data is available on Raspberry Pi, print it to PC
  if (mySerial.available())
    Serial.write(mySerial.read());
  // If data is available on PC, print it to Raspberry Pi
  if (Serial.available())
    mySerial.write(Serial.read());
}
Connect the Arduino to the Raspberry Pi:
Raspberry Pi Pins used:
  • 1 : 3.3V
  • 6 : Ground
  • 8 : UART TXD
  • 10: UART RXD
CD4050 Pins used:
  • 1 : VDD (3.3V)
  • 2 : Output A
  • 3 : Input A
  • 4 : Output B
  • 5 : Input B
  • 8 : VSS (Ground)
If you then open your Serial Monitor on you PC, and set the baudrate to 57600 you should get the following message:
Connected to PC
If you then reboot your Raspberry Pi, the console messages should be echoed back to the Serial Monitor running your PC and leave you with a login prompt from the getty:
If you change ‘No Line Ending’ to ‘Newline’ you can enter your username and are then prompted for a password. Unfortunately the password doesn’t work. I still need to figure out why it’s not accepting my password. I suspect it’s to do with a line termination character or something.
Next step to connect this with a Serial library on the Raspberry Pi to talk to the Arduino. Maybe using pySerial like Dr Monk did in his Raspberry Pi and Arduino project. You should be able to use this in exactly the same way after disabling the console and getty and using ttyAMA0 as the serial port.

วันพุธที่ 24 เมษายน พ.ศ. 2556

Raspberry Pi and Arduino: Serial Communication Over GPIO pins


Jan192013
 
Just a picture of the Raspberry Pi running
Just a picture of the Raspberry Pi running
I recently received a Raspberry Pi from a friend and I wanted to see if I could tie it to one of my Arduino boards without using a USB cable. The tutorials I found were helpful and relatively simple but were missing some details that I thought would be helpful. So I’ve decided to make a (hopefully) more simple tutorial to get people up and running with serial on the Raspberry Pi’s GPIO pins.
Required hardware:
The first thing is to get your Raspberry Pi up and running. I used this wiki page to get an operating system (Raspian Wheezy for me) installed in my SD card. I had to use the “flashnul” software because the Win32DiskImager software used in the “easy way” wouldn’t recognize my SD card.
Once that is done you can plug a keyboard and a monitor into your Raspberry Pi (you don’t need a mouse for what we’re doing). I used the yellow composite cable port to connect my Pi to an old portable DVD player. Plug the power cable (micro USB) into the Raspberry Pi. When it starts for the first time you will see a configuration settings screen. I followed the steps in this tutorial to get mine setup.  Additionally, I enabled the SSH server because I didn’t want to have so many things plugged in when using my Pi. When you’ve finished with the configuration and you’ve rebooted your Pi it will present you with a login screen.  If you had plugged in an ethernet cable to your Pi you should have seen an IP address during boot. I use this IP address to SSH into my Pi from my desktop computer. The login name is “pi” and the password is the one you setup during configuration. Log in to your Pi (I logged in via SSH).
Note: don’t run the “startx” command after you log in. You can do everything you need for this tutorial from the command line.
Now you can begin installing the junk you need to get going with serial over GPIO.
The first thing I did was update the Pi. press “y” when prompted: sudo apt-get update && sudo apt-get upgrade
The next thing is to get Vim installed: sudo apt-get install vim
Now for the actual serial over GPIO configuration…
Use vim to comment (#) this line out of /etc/inittab (it was the last line in my inittab file):
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
so that line becomes:
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
I think I rebooted at this point although I’m not sure it matters: sudo reboot
Then use vim to remove this section from /boot/cmdline.txt
console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
Be careful to remove only that.
Reboot your raspberry pi: sudo reboot
Then install minicom: sudo apt-get install minicom
Now your Raspberry Pi is ready software-wise.
Load this sketch onto your Arduino:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
byte num = 0;
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  if (Serial.available())
  {
    num = Serial.read();
    Serial.print("character recieved: ");
    Serial.println(num, DEC);
  }
}
After that’s been loaded onto the Arduino, turn them both off and wire them up according to this diagram:
Raspberry Pi to Arduino serial wiring diagram
created with draw.io
Also this might help:
GPIO Header - Raspberry Pi
Additionally, there are some pictures of my wiring setup at the bottom of the page.
Double check your wiring. I’ve heard that you can break the crap out of your Raspberry Pi if you do it wrong.
I’m not sure the order matters, but turn on your Raspberry Pi and then turn on your Arduino. Login to your Pi and enter the following command to connect to the Arduino:
minicom -b 9600 -o -D /dev/ttyAMA0
Now whatever you type is sent to the Arduino and the Arduino responds with the decimal format of the character that it received (see asciitable.com)
minicom screenshot
Also, to get out of minicom do: ctrl-a q
Success! Your Raspberry Pi is now communicating with your Arduino over the GPIO pins.
This might be useful for testing, but in order to do something more useful you might want to communicate with the Arduino using Python.
Exit out of  minicom: ctrl-a q
Run this command to install the necessary python serial library: sudo apt-get install python-serial
Then you can write a python program like this to send data to the Arduino over the serial line:
?
1
2
3
4
5
6
7
8
9
10
11
import serial
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
ser.open()
 
ser.write("testing")
try:
        while 1:
                response = ser.readline()
                print response
except KeyboardInterrupt:
        ser.close()
Run the program with: python
And your output should look something like this:
Python serial program output
press ctrl-c to exit the program.
Well that’s all for now. Have fun and let me know in the comments if you have any questions.
Some pictures of the process:
the full setup
the full setup
Raspberry Pi wiring
Raspberry Pi wiring
Arduino wiring
Arduino wiring
Breadboard wiring
Breadboard wiring with Logic Level Converter