3

I apologize for the double post, but I asked this question earlier and somehow it does not appear under my user and I can't make any edit or comments to it: Trouble with UART Serial Communication with Raspberry Pi and Arduino Mega

I'm trying to get the Raspberry Pi to communicate with the arduino and have been following this guide : https://oscarliang.com/raspberry-pi-and-arduino-connected-serial-gpio/

I've followed the instructions for the disabling of getty and using minicom. I've also followed the code in the guide as follows:

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

void loop() {
   if (Serial.available() > 0) {
      int incoming = Serial.read();
      Serial.print("character recieved: ");
      Serial.println(incoming, DEC);
  }
}

From what I understand, when I type a character into the Pi, it will be received by the Arduino and send the ASCII code back. When I uploaded it into the Arduino and booted up minicom without typing anything, there is already output being shown continuously every 1 second or so. When I type into the console it does work and show the corresponding ASCII code, but the arduino seems to be always receiving data. Is this supposed to be the case?

character recieved: 1
character recieved: 1
character recieved: 0
character recieved: 0
character recieved: 0
character recieved: 0
character recieved: 0
character recieved: 2
character recieved: 3
character recieved: 81
character recieved: 3
character recieved: 3
character recieved: 46
character recieved: 32
character recieved: 65   << when I typed a

Using Raspbian GNU/Linux 7 (wheezy) Version 7

uname -a : Linux raspberry pi 3.18.11+ #781 PREEMPT .. armv61 GNU/Linux

Edit: I have checked /boot/cmdline and have commented out as per the guide

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait 

Also, sudo grep -R serial /etc/systemd/system does not show anything.

Lastly, I've used a voltage level-shifter as the tutorial suggested, but still the garbage values persist.

Edit 2: I'm starting to think maybe the connections are not correct. Here is a picture of my level-shifter( oops realised I can't post more than 2 links)

enter image description here

Top to Rasp pi (right to left):

Green: Connected to Pin 1

Yellow: Connected to pin 8

Purple: Connected to pin 10

Blue: Connected to Pin 6

Bottom to Arduino(right to left):

Red: Connected to 5V

Yellow: to RX2 pin 17

White: to TX2 pin 16

Black: To GND

  • If you are using "wheezy" then /etc/inittab will be there, systemd was incorporated in Raspbian 8 (jessie). raspi-config should know the difference and disable the console correctly though. – goldilocks Sep 22 '16 at 13:34
  • yes. I have eddited /etc/inittab as well. removing the line : T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 – John Perry Sep 22 '16 at 13:35
  • 1
    If after that and a reboot the problem persists, it is something else. I think connecting the Pi's TX directly to the Arduino's RX is sketchy; there is no guarantee it is going to work consistently (although it may, I've never tried it without a level shifter). – goldilocks Sep 22 '16 at 13:37
  • 1
    @goldilocks Yep, that's what I was talking about when I asked if the OP used a a voltage level converter, not just a couple of resistors on the RPi's RX pin. – Dmitry Grigoryev Sep 22 '16 at 13:49
  • 1
    @goldilocks I am using a level shifter as shown above. – John Perry Sep 22 '16 at 14:05
1

Found a report on similar behavior (getting invalid characters along with valid ones) posted on this thread.

My hunch is there is some line noise which is showing up as data on the rx pin of the Arduino making it send out responses.

Few things i'd suggest to isolate or identify the problem

  • check the port settings on mini com (baud rate seems OK, but double check parity, bits per byte, handshake, stop bits)

  • try increasing /decreasing the baud rate and see if the problem persists. depending on the Atmega clocking frequency the error % in the baud rate may cause problems in transmission/reception. this table nicely shows the error % for different clocking frequencies

  • Disconnect the arduino and create a loop back on the pi side (short the tx and rx). use minicom to send and verify you get the same and only the characters you sent. I doubt this is the cause of the problem as the first part of the string character received seems to be coming intact without getting garbled.

  • I've tried the loop back on the Pi, and i realised that I still receive garbage value other than the charcaters i send. This could be the problem – John Perry Sep 23 '16 at 6:30
  • looked around, found a few tips probably worth a try common ground, bad power supply, design changes on pi 3, overclocking related problems – Shreyas Murali Sep 23 '16 at 7:06
  • found a complete example too for the time being i'd recommend trying a usb to serial adapter if you have one lying around until you can find the root cause or check the setup with raspbian jessie if you have spare card didnt find anything promising on the PreArm: RC not calibrated though – Shreyas Murali Sep 23 '16 at 7:06
  • The OP didn't receive any valid characters, the code for a is 97, not 65. And I believe 9600 is one of those baudrates which are almost always a safe choice: it's in the green in your table as well. – Dmitry Grigoryev Sep 23 '16 at 7:38
0

Okay, I've managed to get your code working between a Pi2 and an Arduino Pro Mini.

The Pro Mini is meant to be a 5V device but seems to work quite happily when powered from 3V3 which means I didn't need a level shifter.

enter image description here

I had to do the following invocations, in the following order.

  1. sudo raspi-config -> Advanced Options -> Serial -> No
  2. Add enable_uart=1 to \boot\config.txt
  3. Reboot

NOTE: The order of the above is important as raspi-config will incorrectly set enable_uart=0.

You can then use minicom or whatever. I used

minicom -D /dev/serial0

Welcome to minicom 2.7

OPTIONS: I18n                                               
Compiled on Jan 12 2014, 05:42:53.                          
Port /dev/serial0, 11:02:00                                 

Press CTRL-A Z for help on special keys                     

character recieved: 48                                      
character recieved: 49                                      
character recieved: 50                                      
character recieved: 51                                      
character recieved: 52                                      
character recieved: 53                                      
character recieved: 97                                      
character recieved: 98                                      
character recieved: 99                                      
character recieved: 100                  

Permissions were as follows

$ ls -l /dev/ttyAMA0
crw-rw---- 1 root dialout 204, 64 Sep 23 11:02 /dev/ttyAMA0
$ ls -l /dev/serial0
lrwxrwxrwx 1 root root 7 Sep 23 10:57 /dev/serial0 -> ttyAMA0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.