0

This question already has an answer here:

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

marked as duplicate by goldilocks Sep 22 '16 at 13:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Which Pi???????? – Milliways Sep 22 '16 at 10:50
  • Please edit your question and indicate the Pi model and the output of uname -a and cat /etc/os-release. – joan Sep 22 '16 at 10:51
  • Are you using a voltage level converter as the tutorial suggests? If you directly connect a 3.3V pin to a 5V pin, receiving garbage is one of the expected side effects. – Dmitry Grigoryev Sep 22 '16 at 12:46
0

An issue with that tutorial is it slightly dated; it's referring to /etc/inittab which does not exist on current versions of Raspbian. The concern is that by default the UART is used as a login console and also gets messages from the kernel, which is probably why you are getting random values at the Arduino (the actual data from the serial console will be much faster than 9600 baud, so it will read as garbage at that rate).

The easiest way to disable this is to use raspi-config; under Advanced Options there should be one to "Enable/disable shell and kernel messages on the serial connection". That should take care of everything; you will have to reboot.

If that doesn't work double check /boot/cmdline.txt for anything including console=ttyAMA0,115000 or console=serial0,115000 and remove it. Beware that file should remain all one line with spaces between the entries, and you do not want to remove the console line which refers to tty1 (and has no baudrate).

Then check sudo grep -R serial /etc/systemd/system; there should be nothing mentioned. If there is and it refers to getty, disable that via systemctl disable serial-getty@ttyAMA0.service or whatever; it should then say symlinks have been removed.

You still have to reboot. In my experience the Arduino and Pi play well together over the serial link with 8N1, you can use it at the full Arduino speed (115200 baud). Echoing is a good trial task because it should be easy to keep them synced as long as you are always reading back on the Pi side. Beware the Pi uses a buffer 1000 times the size of the one on the Arduino. If you need a foolproof way to resync, connect the reset pin on the Arduino (not sure if all models have them) to a Pi GPIO. I do this via a level shifter, but if you don't have one you could try it directly; the reset has to be kept pulled high, and will reset the Arduino when dropped low. Have it send some kind of pattern in setup() if you then need a method to confirm and get back on the same page.

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