How to Read Serial Data to Rasberry Usb

Today's the last day of my summer holiday, and I had some free time on my hands. So I decided to see if I could get my Arduino Uno and Raspberry Pi to talk to each other. It turned out the chore was fifty-fifty easier than my previous Pi to RS-232 project – all that was needed between the two devices was some jumper wire and ii ane kOhm resistors to class a voltage divider between Arduino TX pin and Pi RX pin – Arduino understands Pi's 3.3V signal levels just fine so Pi TX to Arduino RX needed no voltage shifting at all.

Important UPDATE! Information technology turns out that the RX pivot on the Arduino is held at 5V even when that pin is not initialized. I doubtable information technology is due to the fact that the Arduino is programmed via these aforementioned pins every fourth dimension you wink information technology from Arduino IDE, and there are external (weak) pullups to keep the lines to 5V at other times. So the method described beneath may exist risky – I advise either add a resistor in serial to the RX pin, or utilize a proper level converter (see this post for details how to accomplish that). And if you do effort the method beneath, never connect the Pi to Arduino RX pin before you have already flashed the program to Arduino, otherwise yous may end up with a damaged Pi!!!

Setting Raspberry Pi upwards for serial communications

In order to use the Pi's serial port for anything else than as a panel, you kickoff need to disable getty (the program that displays login seen) by commenting the serial line out of Pi's /etc/inittab:

                      1:2345:respawn:/sbin/getty 115200 tty1 # Line below commented out # 2:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 3:23:respawn:/sbin/getty 115200 tty3 4:23:respawn:/sbin/getty 115200 tty4 5:23:respawn:/sbin/getty 115200 tty5 half-dozen:23:respawn:/sbin/getty 115200 tty6                  

If you don't want the Pi sending stuff over the serial line when information technology boots, you tin also remove the statements console=ttyAMA0,115200 and kgdboc=ttyAMA0,115200 from /boot/cmdline.txt. You'll need to reboot the Pi in order for the changes to take effect.

If you have a 3.3V compatible serial adapter in your reckoner (or a MAX3232 and normal RS-232 adapter, in which case see my tutorial on building 1), it's a good fourth dimension to try out if everything is working on the Pi side. Install minicom (sudo apt-get install minicom) and attach is to serial final:

                      minicom -b 9600 -o -D /dev/ttyAMA0                  

Then open Putty or similar series terminal on PC side. Everything you blazon into minicom should appear in the serial concluding on PC side, and characters typed to serial terminal should appear on minicom.

Connecting Arduino Uno to Raspberry Pi

In that location's basically ii ways to link the Arduino to the Pi. Easier route would exist to just plug Arduino into the Pi via USB. The USB to serial bridge should be automatically recognized and be available at /dev/ttyACM0. Simply if you desire to do it the hard manner like I did, you lot tin can besides connect the Pi GPIO pins (seen on the right, three.3V not needed this fourth dimension) to Arduino:

  1. Connect grounds of both devices (triple check first that they are the grounds!)
  2. Connect Pi TX to Arduino RX (series resistor recommended, only connect after flashing, never flash with Pi continued!)
  3. Connect Arduino TX to Pi RX via a voltage devider

As a voltage divider, I used a 1 kOhm resistor between the Arduino TX and Pi RX, and some other 1 kOhm betwixt Pi RX and ground. That mode, the 5V Arduino signal voltage is effectively halved. Connect the resistor ladder commencement, so the Pi RX betwixt the two resistors, then there's at no point a voltage over 3.3 volts that could damage the Pi! You tin see the connection in action here.

Communication betwixt Pi and Uno

For RaspPi side, I'd recommend minicom (see the control-line higher up) for testing, and pySerial (sudo apt-go install python-serial) for interaction. Using Python, you tin can easily make the Pi do lots of interesting things when commands are received from Arduino side. Here'south a simple ROT-13 application that works with the GPIO serial interface:

                      #!/usr/bin/env python  import series import cord  rot13 = string.maketrans( \     "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz", \     "NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")  test=series.Series("/dev/ttyAMA0",9600) test.open up()  try:     while True: 		line = test.readline(eol='\\r') 		test.write(string.translate(line, rot13)) 		 except KeyboardInterrupt:     pass # do cleanup hither  test.close()                  

For the Arduino, you lot can you the serial capabilities of Arduino surroundings. Here's a simple program that echoes back everything that is sent from Pi to Arduino:

                      void setup() {   Serial.begin(9600); }  void loop() {   int incomingByte;      if(Serial.available() > 0) {     // read the incoming byte:     incomingByte = Serial.read();      // echo     Serial.write(incomingByte);    } }                  

Notation that you shouldn't use these programs together, or nothing happens (and you lot won't come across anything either) – Pi side programs can be tested with a PC serial adapter and Putty, and Arduino side programs with minicom running in the Pi.

That's information technology for this fourth dimension. Now that I have the two things communicating, I think I'll practice something useful with the link next. Maybe a simple Arduino "HDMI shield" using RaspPi!

gaglianoandill.blogspot.com

Source: https://codeandlife.com/2012/07/29/arduino-and-raspberry-pi-serial-communication/

0 Response to "How to Read Serial Data to Rasberry Usb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel