How to run python script on Arduino

I have been researching a lot, and still have not gotten an answer!

Project Detail: I have set up a serial communication between the Arduino and python.

I collect data from Yahoo Finance with Python and send it over to Arduino for further actions such as displaying the information on an LCD screen...

import yfinance as yf
import serial
from time import sleep

arduino = serial.Serial(port='/dev/cu.usbmodem14301', baudrate=9600)

What I want is once I turn on the Arduino (connecting it to my Mac via USB) it automatically executes the python script without me going to the terminal and running the python script.

Previous Research:

  • I tried to use system(python3 it did not work.
  • There are methods such as PyMite which I believe is too complicated for my purpose.
  • I get the idea that Arduino can not make the computer run a program unless I use a "proxy" like keystroke.

Hopefully, there is an easy solution.

I really appreciate any idea! Thank you.

This is one option. You can use of the Arduino boards that can emulate a keyboard, then have it send a special keyboard shortcut to your computer. You then need to have the computer configured to recognize this keypress and run the Python script.

Although I know of a couple of easy methods for doing this on Windows, I don't have a lot of experience with macOS, so I can't give any recommendations. But I'm certain that there are ways to accomplish this on macOS, whether it be via a native capability of the operating system (as the Windows shortcuts have) or a 3rd party application (e.g., AHK on Windows).

Thank you vey much! Do you mind sharing what are the Window shortcuts maybe I can find an equivalent one in Mac too?

In Windows, if you right click on a shortcut and the select "Properties" from the context menu, you'll see it has a "Shortcut key" configuration option. You can click on that field and then press any combination of keys on your key board. After that, when you press that keyboard shortcut, it will start the Windows shortcut.

I don't know whether macOS has an equivalent capability.

Another option is to run the script as a service and monitor when the Arduino is plugged in.

I have no experience in writing services (Daemon in Unix terminologi) for Mac, but if it's anything like in Linux it's very doable without investing too much effort.

From a security perspective, having something plugged in on USB automatically triggering the launch of a background running process is considered a security risk.

So it's not straightforward and requires some configuration work. You need a background running service (user agent) that monitors whenever your specific USB device is plugged in and then launches an App.

you can have a look there for a start

PS: if you search on the internet, don't believe any post telling you need to install macCleaner, macSomething or whatever, you are likely going to install something nasty. just don't install anything untrusted.

Not sure what you mean by that.
You turn on your arduino - by hand? So why not go to the mac starting - what?

You collect data from yahoo finance and I quote your words

I collect data from Yahoo Finance and send it over to Arduino
What does the arduino do with the collected data????
And why is it nescessary to do anything with your MAC if you have sended the data already to the arduino???

this is a very different description from what you have given above.
The arduino shall initiate starting a program on your mac-computer?
Is this what you want?

Well one idea I have is to use a ESP32-module instead of an arduino
The ESP32-module connects to your WLAN-Router and then sends a UDP-Packet to your computer. On your MAC is running a python-script that can receive UDP-messages and if the message is some kind of special message the python-script starts your program.

A python-script should be able to receive serial messages to.

import serial
ser = serial.Serial('/dev/ttyACM0')
ser_bytes = ser.readline()

Anyway you should use at least write ten sentences to explain what you really want to do
Your title "running a python-script on an arduino" leads into a completely wrong direction.
There is micro-python that can run on several microcontrollers but NOT on an arduino uno or Arduino Mega because the have too less memory.

best regards Stefan

Thank you for the reply. Another user is suggesting the same. I will certainly look into that.

I edited my post, hopefully it's clearer.

But you are right, I want to initiate my python program once I connect my Arduino (via USB) to my MAC.

Once that is done, the python will send the data from Yahoo Finance (using yahoo finance library) over to the Arduino. What I currently do is going over the terminal and running python3 manually.

It totally makes sense why it's restricted.

Thank you for the resource, I will look into that tonight.

I still don't get it because you are so short on words.

When your Arduino gets connected to your MAC-computer with an USB-cable you want to start a python-script?

Why on earth do you want to plug in plug / out your arduino all the time
if you want to start a python-script???
OK let's start a ping-pong game
to increase the numbers of postings:

Post a single letter of your answer.
Then wait for my answer begging for the second letter - repeat (10000 times until full answer is given)

Don't enjoy this game? OK so how about writing
all steps in detail that explain everything?

  1. You plug in your Arduino
  2. Arduino-program starts running
  3. Arduino-programm is doing what?
  4. your MAC-computer shall run a python-script that sends data to your ardiunio?
  5. Your arduino-program is doing what with the received data?

Well, I actually like ping-pong. Currently:

  1. I am plugging the Arduino via USB; the Arduino has an LCD screen and a stepper motor as effectors.
  2. Arduino program waits for serial communication.
  3. I have to run the python script from the terminal.
  4. Python sends data to the Arduino.
  5. Arduino uses that data for display on an LCD and moving a stepper motor which is not important for the purpose of this post.

I want to automate step 3!

Run your Python script after booting your computer through Autostart, let it wait for Arduino to connect.

That's it. start the python-script when you boot your computer.
The python-script waits for a command coming from the arduino.
If you plug-in your arduino and the arduino starts its program the arduino program sends some kind of "hello I'm ready to receive"-message to your computer.
The python-script waits for this message and only if it has received this message it starts sending the yahoo-data.

Well another example for as soon as the overall functionality is described:

  1. in normal words
  2. in detail

POP! The solution is here.
best regards Stefan

1 Like

I disagree on many front

  • The ask was clear when I read it

  • the Python script cannot listen to the serial port for a special message, the port does not exist until you plug the arduino in. So your Python script needs to check if this serial port gets open, then connect (which reboots the arduino), then get the handshake

  • the solution you propose, Having a process running and consuming resources (polling to know if the device is connected - how often per second?) is a poor engineering solution if you can have the system notify you of such an event and trigger the launch of your app.

Where is your proposal?

I agree, that a solution that needs no "all_the_time_polling" is even better.
I have running a Autohotkeyscript on my Computer that checks for about 20 different things all the time. Hotkeys for starting applications, hotstrings, parts of the script looking for certain applications beeing started to translate keypresses into other keypresses, etc. etc. processor-load 0%

If I additionally start a python script that listens for incoming UDP-messages
the processorload is 0% too.
So from a practical point of view still a good working solution.
(the system has to do some polling in the backround too for the event of plugin in the arduino creating the com-port).
The idea of a keypress to initiate the data-sending is good.
So I did some googling and came up with this

If you can go beyond this solution with even less processor-load avoiding polling I will pull my hat and bow.
best regards Stefan

Having a service that polls for some condition really is par for the course. It's partly what they're made for. It happens all the time in built in system services as well.

Checking if a certain serial device exists is gonna use a trillionth of the computers resources. The right engineering solution is what works and is practical.

You need to define practical. I guess it depends on skills and/or effort you are willing to put in.

A user agent service would be probably more generic in my view to listen to system events.

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