Python communicate with Arduino

Hello all,

I am interested in controlling an Arduino via serial with Python 3 on a Mac running MacOS Monterey 12.5.

Any help would be appreciated
@anon44338819

You might be interested in this RPC library.

Here is a quick start example that shows how the library works.

If you can make a start and show us what you have we might be able to fill in a few gaps. Can you also tell us what it is on the Arduino that you are controlling, whether you expect a response from the Arduino and if you require or prefer some kind of protocol. Which end is it you need help with, the Python or the Arduino.

I have a relay hooked up to the Arduino on pin 13. I want to be able to control it from Python. I know code for the Arduino, but not Python.
It would work like this:

  • The user inputs info to the Python program, or the program automatically turns the relay on or off by sending a 1 (on) or 0 (off) via Serial to the Arduino. Help me with this.
  • The Arduino detects the signals and turns the relay on or off according to the signal. I know how to code this.

I need more help for that.

There are many tutorials online about using PySerial and communicating with an Arduino.

What IDE are you using to program the Python code?

Do you know how to get a Python program to read data in from the computer keyboard? If you don't know how to do this, that's a Python question and not an Arduino question.

You can't truly expect the Arduino board to help you program the Python side, but if you want help here you will need to post both the Python and the Arduino code you have and explain any issues you are having.

I am using the Python IDLE 3.10.4

I am using the input() command to use user input from the IDLE or Terminal.

Have you seen Demo of PC-Arduino comms using Python? It might give you the idea.

I'm still looking more for CODE.

There is a python program and a matching arduino sketch in the opening post; there is also some code in post #5 in that topic.

Thanks! I still haven't looked into the full code, but I will later.

In the link to the quick start, there is also a link to the python side of the library.

Essentially, this is what it looks like on the Arduino.

#include <simpleRPC.h>

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

void loop() {
  interface(
    Serial,
    digitalRead,
      "digital_read: Read digital pin. @pin: Pin number. @return: Pin value.",
    digitalWrite,
      "digital_write: Write to a digital pin. @pin: Pin number. @value: Pin value.");
}

And this is what it looks like on the computer.

from simple_rpc import Interface

interface = Interface('/dev/ttyACM0')  # Make sure to select the right port here.

value = interface.digital_read(8)  # Read from pin 8.
interface.digital_write(13, True)  # Turn LED on.

You can add arbitrary functions to the interface call on the Arduino to expose them to the computer.

If your working with tkinter to create your interface, which comes packaged with Python 3, then you would probably start building your app by recognizing available ports and displaying the choices. From there you would probably go to opening and closing ports correctly before beginning a few small transmit receive tests. To get you started with the above, if it is what you had in mind, then here is a fairly good entry point.

from tkinter import *
from tkinter import ttk
import serial.tools.list_ports


root=Tk()

root_width=500
root_height=250

port_list=[]
ports = list(serial.tools.list_ports.comports())

for p in ports:
	port_list.append(p.name)

ports_cmb = ttk.Combobox(root,values=port_list,width=25)
ports_cmb.grid(row=0,column=0,padx=10,pady=10)
ports_cmb.current(0) 

root.mainloop()

Thanks! That worked

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.