xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 3:32 pm

Hello everyone, after some hours googling and googling I decide to write my issue here. I am another one that is trying to connect Arduino and Raspberry Pi 3 using a common USB cable. I do not use any GPIO pins on my Raspberry, Just plugin the USB cable from the Arduino to one of the four USB ports in the Raspberry Pi board. I have installed the latest software.

I have made many changes but here is the last one. I am using HDMI cable to work using my monitor and not via SSH. Right now in the /boot/config.txt file i have added the following

dtoverlay=pi3-disable-bt
enable_uart=0 (tried with 1 too)

Also I hvae used raspi-config to disable login sheel and enable serial hardware. That is all that i have done. My code works with my desktop computer but not with the RPI so its not a code issue. If anyone knows i would appreciate if you could tell me what exactly to disable or enable because i have read tooooons of stuff and I dont like changing things without knowing if it is right. thank you !!

drgeoff
Posts: 10206
Joined: Wed Jan 25, 2012 6:39 pm

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 3:40 pm

What exactly is your aim in connecting the Arduino and the RPi?

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 3:51 pm

The Arduino should show up as /dev/ttyACM0. You can open it like a serial port. I use both Python and C to access my Mega 2560 through the USB connection.

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 3:54 pm

Transmiting some strings from the rpi to the arduino such as <32,52,63,73>. I dont have much time so i need something with the hardware i have right now. I bet there are more ways to do that .

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 3:55 pm

SurferTim wrote:
Fri Feb 02, 2018 3:51 pm
The Arduino should show up as /dev/ttyACM0. You can open it like a serial port. I use both Python and C to access my Mega 2560 through the USB connection.
I know this, i can see that with ls /dev*. I just can not receive in my arduino what the python code sends.

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 4:15 pm

The Arduino will receive and send over Serial. Try something like this to test. This just echoes what it receives back to the sending program.

Code: Select all

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

void loop() {
  if(Serial.available()) {
    char ch = Serial.read();
    Serial.write(ch);
  }
}

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 4:28 pm

Code: Select all

pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ ls /dev*
autofs           hidraw2           mmcblk0p1           ram4     tty10  tty29  tty47  tty8       vcs7
block            hidraw3           mmcblk0p2           ram5     tty11  tty3   tty48  tty9       vcsa
btrfs-control    hwrng             mqueue              ram6     tty12  tty30  tty49  ttyACM0    vcsa1
bus              initctl           net                 ram7     tty13  tty31  tty5   ttyAMA0    vcsa2
cachefiles       input             network_latency     ram8     tty14  tty32  tty50  ttyprintk  vcsa3
char             kmsg              network_throughput  ram9     tty15  tty33  tty51  ttyS0      vcsa4
console          log               null                random   tty16  tty34  tty52  uhid       vcsa5
cpu_dma_latency  loop0             ppp                 raw      tty17  tty35  tty53  uinput     vcsa6
cuse             loop1             ptmx                rfkill   tty18  tty36  tty54  urandom    vcsa7
disk             loop2             pts                 serial   tty19  tty37  tty55  usb        vcsm
fb0              loop3             ram0                serial0  tty2   tty38  tty56  vchiq      vhci
fd               loop4             ram1                serial1  tty20  tty39  tty57  vcio       watchdog
full             loop5             ram10               shm      tty21  tty4   tty58  vc-mem     watchdog0
fuse             loop6             ram11               snd      tty22  tty40  tty59  vcs        zero
gpiochip0        loop7             ram12               stderr   tty23  tty41  tty6   vcs1
gpiochip1        loop-control      ram13               stdin    tty24  tty42  tty60  vcs2
gpiochip2        mapper            ram14               stdout   tty25  tty43  tty61  vcs3
gpiomem          mem               ram15               tty      tty26  tty44  tty62  vcs4
hidraw0          memory_bandwidth  ram2                tty0     tty27  tty45  tty63  vcs5
hidraw1          mmcblk0           ram3                tty1     tty28  tty46  tty7   vcs6
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $
here are the results from my devices.

and here is the code that i am using to send data to the arduino

Code: Select all


import time
import serial

# Create a new serial with the arduino.
ser=serial.Serial('/dev/ttyACM0', 9600)

while True:
        if(ser.isOpen()):
                ser.write("<32>")
                incData = ser.readline()
                print incData
                #time.sleep(0.5)
I edit my post, sorry for the bad copy paste

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 4:48 pm

SurferTim wrote:
Fri Feb 02, 2018 4:15 pm
The Arduino will receive and send over Serial. Try something like this to test. This just echoes what it receives back to the sending program.

Code: Select all

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

void loop() {
  if(Serial.available()) {
    char ch = Serial.read();
    Serial.write(ch);
  }
}
I have uploaded your sketch to my arduino and with the following code to the rpi nothings comes back.||

Code: Select all

pi@raspberrypi:~/Desktop $ ls
pi@raspberrypi:~/Desktop $ cd Q
-bash: cd: Q: No such file or directory
pi@raspberrypi:~/Desktop $ cd ../Project/QuadcopterVisualServoing/RPi_CV/
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ ls
ctrack.py           find_s_des.py  input.png  new.py            README.txt          testCode.py
des_img_old.py      fps_test.py    mine3.py   picam_img_cap.py  readVideoStream.py  webcam_img_cap.py
desiredsvector.png  ibvs_ctrl.py   mine.py    radiusCalc.py     serialTest.py
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ python serialTest.py
^CTraceback (most recent call last):
  File "serialTest.py", line 14, in <module>
    incData = ser.readline()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 472, in read
    ready, _, _ = select.select([self.fd, self.pipe_abort_read_r], [], [], timeout.time_left())
KeyboardInterrupt
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ clear
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ ls /dev*
autofs           hidraw2           mmcblk0p1           ram4     tty10  tty29  tty47  tty8       vcs7
block            hidraw3           mmcblk0p2           ram5     tty11  tty3   tty48  tty9       vcsa
btrfs-control    hwrng             mqueue              ram6     tty12  tty30  tty49  ttyACM0    vcsa1
bus              initctl           net                 ram7     tty13  tty31  tty5   ttyAMA0    vcsa2
cachefiles       input             network_latency     ram8     tty14  tty32  tty50  ttyprintk  vcsa3
char             kmsg              network_throughput  ram9     tty15  tty33  tty51  ttyS0      vcsa4
console          log               null                random   tty16  tty34  tty52  uhid       vcsa5
cpu_dma_latency  loop0             ppp                 raw      tty17  tty35  tty53  uinput     vcsa6
cuse             loop1             ptmx                rfkill   tty18  tty36  tty54  urandom    vcsa7
disk             loop2             pts                 serial   tty19  tty37  tty55  usb        vcsm
fb0              loop3             ram0                serial0  tty2   tty38  tty56  vchiq      vhci
fd               loop4             ram1                serial1  tty20  tty39  tty57  vcio       watchdog
full             loop5             ram10               shm      tty21  tty4   tty58  vc-mem     watchdog0
fuse             loop6             ram11               snd      tty22  tty40  tty59  vcs        zero
gpiochip0        loop7             ram12               stderr   tty23  tty41  tty6   vcs1
gpiochip1        loop-control      ram13               stdin    tty24  tty42  tty60  vcs2
gpiochip2        mapper            ram14               stdout   tty25  tty43  tty61  vcs3
gpiomem          mem               ram15               tty      tty26  tty44  tty62  vcs4
hidraw0          memory_bandwidth  ram2                tty0     tty27  tty45  tty63  vcs5
hidraw1          mmcblk0           ram3                tty1     tty28  tty46  tty7   vcs6
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ ls /dev*
autofs           hidraw2           mmcblk0p1           ram4     tty10  tty29  tty47  tty8       vcs7
block            hidraw3           mmcblk0p2           ram5     tty11  tty3   tty48  tty9       vcsa
btrfs-control    hwrng             mqueue              ram6     tty12  tty30  tty49  ttyACM0    vcsa1
bus              initctl           net                 ram7     tty13  tty31  tty5   ttyAMA0    vcsa2
cachefiles       input             network_latency     ram8     tty14  tty32  tty50  ttyprintk  vcsa3
char             kmsg              network_throughput  ram9     tty15  tty33  tty51  ttyS0      vcsa4
console          log               null                random   tty16  tty34  tty52  uhid       vcsa5
cpu_dma_latency  loop0             ppp                 raw      tty17  tty35  tty53  uinput     vcsa6
cuse             loop1             ptmx                rfkill   tty18  tty36  tty54  urandom    vcsa7
disk             loop2             pts                 serial   tty19  tty37  tty55  usb        vcsm
fb0              loop3             ram0                serial0  tty2   tty38  tty56  vchiq      vhci
fd               loop4             ram1                serial1  tty20  tty39  tty57  vcio       watchdog
full             loop5             ram10               shm      tty21  tty4   tty58  vc-mem     watchdog0
fuse             loop6             ram11               snd      tty22  tty40  tty59  vcs        zero
gpiochip0        loop7             ram12               stderr   tty23  tty41  tty6   vcs1
gpiochip1        loop-control      ram13               stdin    tty24  tty42  tty60  vcs2
-bash: pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV: No such file or directory
gpiochip2        mapper            ram14               stdout   tty25  tty43  tty61  vcs3
gpiomem          mem               ram15               tty      tty26  tty44  tty62  vcs4
hidraw0          memory_bandwidth  ram2                tty0     tty27  tty45  tty63  vcs5
hidraw1          mmcblk0           ram3                tty1     tty28  tty46  tty7   vcs6
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ autofs           hidraw2           mmcblk0p1           ram4     tty10  tty29  tty47  tty8       vcs7
-bash: autofs: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ block            hidraw3           mmcblk0p2           ram5     tty11  tty3   tty48  tty9       vcsa
-bash: block: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ btrfs-control    hwrng             mqueue              ram6     tty12  tty30  tty49  ttyACM0    vcsa1
-bash: btrfs-control: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ bus              initctl           net                 ram7     tty13  tty31  tty5   ttyAMA0    vcsa2
-bash: bus: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ cachefiles       input             network_latency     ram8     tty14  tty32  tty50  ttyprintk  vcsa3
-bash: cachefiles: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ char             kmsg              network_throughput  ram9     tty15  tty33  tty51  ttyS0      vcsa4
-bash: char: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ console          log               null                random   tty16  tty34  tty52  uhid       vcsa5
-bash: console: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ cpu_dma_latency  loop0             ppp                 raw      tty17  tty35  tty53  uinput     vcsa6
-bash: cpu_dma_latency: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ cuse             loop1             ptmx                rfkill   tty18  tty36  tty54  urandom    vcsa7
-bash: cuse: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ disk             loop2             pts                 serial   tty19  tty37  tty55  usb        vcsm
-bash: disk: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ fb0              loop3             ram0                serial0  tty2   tty38  tty56  vchiq      vhci
-bash: fb0: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ fd               loop4             ram1                serial1  tty20  tty39  tty57  vcio       watchdog
-bash: fd: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ full             loop5             ram10               shm      tty21  tty4   tty58  vc-mem     watchdog0
-bash: full: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ fuse             loop6             ram11               snd      tty22  tty40  tty59  vcs        zero
-bash: fuse: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ gpiochip0        loop7             ram12               stderr   tty23  tty41  tty6   vcs1
-bash: gpiochip0: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ gpiochip1        loop-control      ram13               stdin    tty24  tty42  tty60  vcs2
-bash: gpiochip1: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ gpiochip2        mapper            ram14               stdout   tty25  tty43  tty61  vcs3
-bash: gpiochip2: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ gpiomem          mem               ram15               tty      tty26  tty44  tty62  vcs4
-bash: gpiomem: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ hidraw0          memory_bandwidth  ram2                tty0     tty27  tty45  tty63  vcs5
-bash: hidraw0: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ hidraw1          mmcblk0           ram3                tty1     tty28  tty46  tty7   vcs6
-bash: hidraw1: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $
-bash: pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV: No such file or directory
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ ls /dev*
autofs           hidraw2           mmcblk0p1           ram4     tty10  tty29  tty47  tty8       vcs7
block            hidraw3           mmcblk0p2           ram5     tty11  tty3   tty48  tty9       vcsa
btrfs-control    hwrng             mqueue              ram6     tty12  tty30  tty49  ttyACM0    vcsa1
bus              initctl           net                 ram7     tty13  tty31  tty5   ttyAMA0    vcsa2
cachefiles       input             network_latency     ram8     tty14  tty32  tty50  ttyprintk  vcsa3
char             kmsg              network_throughput  ram9     tty15  tty33  tty51  ttyS0      vcsa4
console          log               null                random   tty16  tty34  tty52  uhid       vcsa5
cpu_dma_latency  loop0             ppp                 raw      tty17  tty35  tty53  uinput     vcsa6
cuse             loop1             ptmx                rfkill   tty18  tty36  tty54  urandom    vcsa7
disk             loop2             pts                 serial   tty19  tty37  tty55  usb        vcsm
fb0              loop3             ram0                serial0  tty2   tty38  tty56  vchiq      vhci
fd               loop4             ram1                serial1  tty20  tty39  tty57  vcio       watchdog
full             loop5             ram10               shm      tty21  tty4   tty58  vc-mem     watchdog0
fuse             loop6             ram11               snd      tty22  tty40  tty59  vcs        zero
gpiochip0        loop7             ram12               stderr   tty23  tty41  tty6   vcs1
gpiochip1        loop-control      ram13               stdin    tty24  tty42  tty60  vcs2
gpiochip2        mapper            ram14               stdout   tty25  tty43  tty61  vcs3
gpiomem          mem               ram15               tty      tty26  tty44  tty62  vcs4
hidraw0          memory_bandwidth  ram2                tty0     tty27  tty45  tty63  vcs5
hidraw1          mmcblk0           ram3                tty1     tty28  tty46  tty7   vcs6
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ cat serialTest.py
# This python sketch gets an input from the keyboard
# and writes it to serial waiting from the arduino to
# pass it rhrough the flight controller.

import time
import serial

# Create a new serial with the arduino.
ser=serial.Serial('/dev/ttyACM0', 9600)

while True:
        if(ser.isOpen()):
                ser.write("<32>")
                incData = ser.readline()
                print incData
                #time.sleep(0.5)
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ # This python sketch gets an input from the keyboardimport time
import serial

# Create a new serial with the arduino.
ser=serial.Serial('/dev/ttyACM0', 9600)

while True:
        if(ser.isOpen()):
                ser.write("<32>")
                incData = ser.readline()
                print incData
                #time.sleep(0.5)pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ # and writes it to sorial waiting from the arduino to
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ # pass it rhrough the flight controller.
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ import time
-bash: import: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ import serial
-bash: import: command not found
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ # Create a new serial with the arduino.
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ ser=serial.Serial('/dev/ttyACM0', 9600)
-bash: syntax error near unexpected token `('
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ while True:
>         if(ser.isOpen()):
-bash: syntax error near unexpected token `)'
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $                 ser.write("<32>")
-bash: syntax error near unexpected token `"<32>"'
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $                 incData = ser.readline()
-bash: syntax error near unexpected token `('
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $                 print incData
Error: no such file "incData"
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ cat serialTest.py
# This python sketch gets an input from the keyboard
# and writes it to serial waiting from the arduino to
# pass it rhrough the flight controller.

import time
import serial

# Create a new serial with the arduino.
ser=serial.Serial('/dev/ttyACM0', 9600)

while True:
        if(ser.isOpen()):
                ser.write("<32>")
                incData = ser.readline()
                print incData
                #time.sleep(0.5)
pi@raspberrypi:~/Project/QuadcopterVisualServoing/RPi_CV $ python serialTest.py

User avatar
B.Goode
Posts: 9244
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 5:27 pm

So the Raspberry Pi script is 'hanging' here:

Code: Select all

      incData = ser.readline()
??

I haven't gone to read the documentation, but what is likely to be treated as a 'line'?

You seem to have sent a sequence of characters, "<32>", which should have been echoed by the sketch running on the Arduino. But there is nothing there to say "this is the end of a line" ?? So ser.readline() has no way of knowing that it should have finished its work...

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 5:48 pm

I think that this is not the problem. Let me explain. First of all I just added a LED on my arduino that blinks when something is received. When I send any sequence from my rpi it does not blink. So I think that the problem is that the arduino is not receiving the data properly. Also, it seems that when i transmit data there are some blinks on my arduino but its not my led. It seems that something is going on there. Um, Also have in mind that the two sketches work fine with my desktop computer. So its not about the code i guess. I remember having the same issue two years ago but i do not remember what i did. This bluetooth with serial communcation seems to make all the communications go crazy....

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 6:02 pm

The readline function waits until it receives a newline character. It is blocking.

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 6:18 pm

Since i am using Serial.println() with my arduino it should be receiving a new line. No?

User avatar
B.Goode
Posts: 9244
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 6:23 pm

My advice was based on the assumption that you were debugging/troubleshooting using the loopback sketch proposed by @SurferTim.

I don't think you have posted your Arduino sketch, so it is difficult to comment...

User avatar
Cancelor
Posts: 759
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 6:34 pm

xmarkx wrote:
Fri Feb 02, 2018 3:32 pm
Hello everyone, after some hours googling and googling I decide to write my issue here. I am another one that is trying to connect Arduino and Raspberry Pi 3 using a common USB cable. I do not use any GPIO pins on my Raspberry, Just plugin the USB cable from the Arduino to one of the four USB ports in the Raspberry Pi board. I have installed the latest software.

I have made many changes but here is the last one. I am using HDMI cable to work using my monitor and not via SSH. Right now in the /boot/config.txt file i have added the following

dtoverlay=pi3-disable-bt
enable_uart=0 (tried with 1 too)

Also I hvae used raspi-config to disable login sheel and enable serial hardware. That is all that i have done. My code works with my desktop computer but not with the RPI so its not a code issue. If anyone knows i would appreciate if you could tell me what exactly to disable or enable because i have read tooooons of stuff and I dont like changing things without knowing if it is right. thank you !!
I don't have the answer for you but I need to point out that the serial port that your USB cable may/maynot produce is not the same as the on board RPi serial port.

I think you need to restore the config.txt to what it was and then use lsusb in a terminal window to identify the USB device .... as I say I'm not sure of the top of my head what to do next.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
Cancelor
Posts: 759
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 6:38 pm

Is this approximately what you have?
RPiduino.jpg
RPiduino.jpg (46.22 KiB) Viewed 10272 times
http://www.instructables.com/id/Raspber ... unication/
Last edited by Cancelor on Fri Feb 02, 2018 6:42 pm, edited 1 time in total.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 6:41 pm

This is the sketch of my arduino:

Code: Select all

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

void loop() {
  if(Serial.available()) {
    char ch = Serial.read();
    digitalWrite(LED_BUILTIN, HIGH);   
  delay(500);               
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);   
  digitalWrite(LED_BUILTIN, LOW);
    Serial.write(ch);
  }
}
Let me also say that when i am just send messages from arduuno to my rpi it works

Arduino sketch that sends data to rpi:

Code: Select all

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

void loop() {
  Serial.println("hello pi");
   
}

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 7:15 pm

Yes, that appears to be the setup I have.

Your first sketch does not send a newline to the RPi unless the RPi sends a newline to the Arduino. The second does.

Edit: What are you using to send the characters to the Arduino? Is it the Arduino IDE?

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 7:19 pm

Guys I think I understood whats going on. It is weird. Sending data from arduino to rpi is easy. However sending data from arduino to rpi then from the rpi to arduno and visa versa is complicated. I think that the arduino must initialize the communication else it does not work. I will test more examples and get back here for more. If you have any ideas you are welcomed to say them. thank you

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 7:20 pm

It isn't difficult at all. How are you sending characters to the Arduino from the RPi? Are you using the Arduino IDE or your own Python code?

xmarkx
Posts: 19
Joined: Mon Apr 17, 2017 12:54 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 7:31 pm

SurferTim wrote:
Fri Feb 02, 2018 7:20 pm
It isn't difficult at all. How are you sending characters to the Arduino from the RPi? Are you using the Arduino IDE or your own Python code?
I have two days trying to figure it out. I am using python code to send data to the arduino. I am send something like <43, 65, 125, 72> and from the arduino side i am parsing the data, remove the <> and print the numbers which are splited by commas. It make sense?

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 8:03 pm

OK, then when you think you have a complete string on the Arduino, like when you receive a '>', then send a newline ('\n' ) to the RPi.

Edit: Or even better, use ser.read() on the RPi.

User avatar
Cancelor
Posts: 759
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 8:14 pm

Yup, makes sense, you must be so close to having it working.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
rin67630
Posts: 855
Joined: Fri Mar 04, 2016 10:15 am

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 8:31 pm

SurferTim wrote:
Fri Feb 02, 2018 3:51 pm
The Arduino should show up as /dev/ttyACM0. You can open it like a serial port. I use both Python and C to access my Mega 2560 through the USB connection.
Only orginal Arduinos show up as /dev/ttyACM0
Chinese clones with a CHM340 show up as /dev/ttyUSB0.

enter dmesg | grep tty and look at the output.
if you see ch341-uart converter now attached to ttyUSB0 you will know that you need to use /dev/ttyUSB0.

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Connect Arduino with Raspberry Pi 3

Fri Feb 02, 2018 9:23 pm

This is Python, so you must open the serial port.

Code: Select all

ser = serial.Serial('/dev/ttyACM0',115200);
If /dev/ttyACM0 did not exist, the Python script would have thrown an error and aborted.

Return to “Troubleshooting”