2

Should I keep Arduino IDE and Serial 9600 open to be able to recieve data sent from python code , Arduino is connected with pi 3 with serial communication ?

tried to send data while the arduino IDE and serial opened and it's going perfectly ,but when closed the IDE i didn't get any response

i connect arduino with stepper motor driver and when flashed code on arduino which listen data from serial 9600 which sent by pi3 , so when i opened the serial i noticed that my motor moved on , but when closed the IDE got no response!

if I should open serial 9600 before sending data from pi3 to arduino what can i modify on the following code to be able to run it automatically :

import time
import serial

ser=serial.Serial(

port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter="60"
ser.write(counter)
time.sleep(5)
ser.close

code on arduino

note: tried to replace if condition about check serial.begin by if the read value !=0 after putting the following 2 lines - after the if - before,But doesn't work!

10
  • Should I keep ... open... What does open mean?
    – joan
    Jun 2, 2017 at 8:02
  • @joan opening of Arduino IDE, and serial window. Jun 2, 2017 at 11:45
  • You need to edit your question and add your Arduino code. You're only showing the Pi side and that isn't going to help much here.
    – stevieb
    Jun 2, 2017 at 12:18
  • @stevieb I already added a link for the arduino code and it's just read from serial what's matter with this pb Jun 2, 2017 at 12:23
  • The only thing I see that I don't use is the if (Serial.available() > 0) line. If you remove that block, does the problem go away?
    – stevieb
    Jun 2, 2017 at 13:48

1 Answer 1

4

I do this all the time. Two-way communications works just fine.

Yes I keep my ports open continuously. UNLESS I am running the IDE. The IDE requires that I not have any other programs trying to use the same serial port.


The code I use on the Arduino to open the port is much more simple than you're showing.

Serial.begin(9600);
while (Serial.available()>0) serIn=Serial.read();

to open it then flush the buffer using a char variable.

Test it out in the serial monitor from your IDE to make sure the Arduino is working correctly before moving on to the Python program on the Raspberry Pi.

Note that the serial monitor will always send an end-of-line \n - so to read single-character commands the best way is like so.

if (Serial.available()>0) {
  serIn=Serial.read()
  if (SerIn=='A') { 
    ...do something...
  }
  while (Serial.available()>0) serIn=Serial.read();
}     

On the Raspberry Pi in python, talking to a Nano, here is the Python code to open the port:

import serial
ser = serial.Serial("/dev/ttyUSB0",9600)
ser.flushInput()

Very simple.

Nano mounted on a breadboard so there is room for voltage dividers and such, then linked to the Rpi3 by the programming cable to send the readings to the Raspberry Pi and to receive instructions to toggle digital outputs which can switch relays.

(I always use single-character instructions. Arduino char 'X' means to shut off the inverter, for instance.)

Python code to process input from the serial port:

while True :
    linein = ser.readline()
    ...etc - to process input from the Arduino.  

Python code to write a single character out the serial port:

ser.write("A")

This sends the single character "A", which is received on the Arduino as a char 'A' in the read loop shown above.

In Python you use double-quotes to denote a character for writing. ser.write does not add an end-of-line.


If you are using an Uno the open sequence will instead be this because they tend to use a different port in Jessie.

import serial
ser = serial.Serial("/dev/ttyACM0",9600)
ser.flushInput()

You can see which ports are there with this bash command :

#ls /dev/tty*

No magic involved. Just very straightforward and simple.


There's only one gotcha: If you are running your Arduino IDE on the Raspberry Pi then you need to make sure you don't interfere with the serial port during program uploading to the Arduino.

This is important but not much of an issue once you have it tuned up and don't use the IDE as often.

My systems run 24x7 for days on end. I rarely need to change the programs now that they are stable. You can see some of the output generated by these systems here: https://www.sdsolarblog.com/montage

4
  • Your answer far away my question scope ,but whatever are you suggesting use /dev/ttyUSB instead of /dev/ttyUSB0 to send data from pi3 to Arduino , because all your answer not in scope , I have only one pi , only one Arduino , no internet access needed , no data required to send from Arduino to pi , all I need to send data from pi to Arduino by serial communication automatically when run command line or run python code or even shall script without any manual interaction . Jun 3, 2017 at 3:28
  • NO, no output when send data from pi to Arduino while closing the arduino ide , serial Windows!! Else as past I got everything well but what's need that send data automatically without manual opening ARDUINO IDE or serial window. Jun 3, 2017 at 4:07
  • Maybe you could help me with a related thread: raspberrypi.stackexchange.com/questions/68532/…
    – marciokoko
    Jun 26, 2017 at 22:13
  • 1
    Truse me on those port names. But also verify. Use this command on the Raspberry Pi to be sure: ls /dev/tty* - and it will show you any ttyUSBx or ttyACMx ports - those are the port names you know will work. If you only have one serial connection it is easy to spot. Here is another trick. Run that command. Then unplug the USB connector. Wait a few seconds then run the command again. It will be gone. Plug it back in and wait a few seconds, then run the command again. It will be back. That is the surest way to know which port Jessie is assigning to it.
    – SDsolar
    Oct 24, 2017 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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