Hot answers tagged

26

It's going to be extremely difficult to get any kind of Python script running directly on the Arduino. The reason is that it's an interpreted language, so you would need the interpreter on-board in addition to the plain text script. There's probably not going to be enough memory for all of that. Your best bet would probably be finding a way to compile a ...


15

Yes, it is (somewhat) possible to program the Arduino using Python. One such project on Github is the Python Arduino Prototyping API v2. It provides very basic functionality such as digital I/O and analog I/O. This can be used for very simple projects. *This project is a bit of a hack at "programming" the board using the serial connection. It passes the ...


8

There is a project which brings a Python virtual machine to micro-controllers, including the Arduino Mega. Here's a quote from the Ardunio Mega README, which gives a feel for what this could be like (though, I've not tested this!): The following is an example session using ipm:: ipm> import avr, sys ipm> avr.ddrA(0xff) ipm> avr.portA(0) #...


6

Parsing on the Arduino can be slow and time-consuming (which is bad if you use clock prescaling or have time-critical tasks), so let's do it in Python. The problem is that you're sending the numbers as ASCII whereas you need to be sending them as raw binary. This is where struct comes in. 3>> import struct 3>> print(struct.pack('>B', 0)) b'\...


6

If I use >BBB instead of >iii... That's because "B" is a byte. So you end up sending 3 bytes: struct.pack("BBB", 1, 2, 3) \x01\x02\x03 "i" is an int and has a size of 4, so you send 4*3 = 12 bytes: struct.pack("iii", 1, 2, 3) \x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00 Serial.read() reads 1 byte at a time, so you need to read 4 bytes for each value. ...


5

On a direct USB CDC/ACM connection (as you get on the Micro) there is no such thing as baud rate. Any baud rate setting performed by the host (the PC) is merely an instruction to the device (the Arduino) to say "I would like you to operate at this speed", not "I would like you to communicate with me at this speed". It is used in situations where the PC ...


4

It looks like from that page you can also use various API's to execute Python directly on the Arduino. I don't see that, at all. All those links show you how to communicate with the Arduino over serial using Python on the host side. You'd need a python interpreter to be able to run python on the Arduino. There is the Python On A Chip project and it seems ...


4

I'm somewhat confused by the flow of your program, so instead I will tell you how I would handle the data. You have a single-byte header, then 7 bytes of data, followed by a single byte checksum. PySerial's read is (I believe) blocking by default, so it's simple to read the next serial character - unlike an Arduino. It also has a "multi-byte" read version ...


4

Both Mega 2560 and Due can handle 4 channels of 1,440 samples per second A-to-D and the least significant byte of the system time stamp sent as a continuous stream of vectors through the USB port to Linux. In the lab I train at, we see almost no variance in time increment between samples or between receiver read operations, indicating that the bottleneck is ...


3

// save some unsigned ints uint16_t SIZE, *inputList, cont = 0; boolean inputsReady = false; void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); //free dynamic array memory free(inputList); You are freeing a NULL pointer which is an undefined operation not necessary here, since it was never allocated. if ...


3

The answer is that it depends on what you are actually trying to accomplish. If there is nothing else to be done on the Arduino then it is fine to have it sit and wait; on the other hand if there is nothing else for the python script to do then it is fine for it to sit and wait, but there is no reason for both of them wait. Arduino delays You don't need to ...


3

I imagine you want to use python because you are already familiar with it, know your way around the syntax, etc. But I would advice against this line of thinking, because microcontrollers need a little more of "low-level" thinking (given the limited resources they generally have) and because of that, it's probably best if you learn some C/C++. The arduino ...


3

Another option to consider is the Arduino Yun. In addition to the AVR chip which most Arduinos have, it also includes a MIPS chip running Linux. Since it's linux, you can easily run python code, and any pure-python package can be easily installed. However, all the interesting input/output capability is on the AVR chip, not on the MIPS chip. Projects such as ...


3

I have used pyserial with great success in controlling the pins on the arduino. Your workstation (laptop, etc) runs a python script that then communicates in real time to the arduino. It's not quite the same as running the script directly on the arduino but I was doing quite a bit more than I thought the arduino could handle (some music analysis, hitting a ...


3

You are not using a serial connection, you are using a USB CDC/ACM connection, and that is a very very different thing. With a serial connection when you send data it gets sent regardless of whether there is anything listening for it - the TX IO pin toggles high and low regardless. However, with a CDC/ACM connection it's only possible to send data when ...


3

You declare incomingchar as a char. In your if you compare that char to a string (double quotes) incomingchar=="g". I think you want to compare it to a char (single quotes) like this: if ( incomingchar == 'g' ) { // do something }


3

Your main problem is in erroneously having a delay in both the sender and the receiver. While this sometimes works by lucky accident, it essentially never does what is hoped for by those who try it. In a system such as this, you should have a delay in the sender only, in order to produce quasi-regular sample intervals, but no delay in the receiver. Rather,...


3

If you don't know the length of the data then you need to be sending start and end markers so your other code can tell where the transmission starts and ends. I usually use < and >, so the data packet might look like <12345678>. You don't have to use those particular characters, just pick something that you know won't show up in your data.


3

In case other people run into similar issues, I'll lay out what I've found today. -The inconsistent output was due to the fact that when you open a serial connection with python, it resets the Arduino and so python can send signals without the arduino having finished its setup() function. If I put in a 1 second delay on the python side, inconsistency stops ...


3

The AVR Arduinos (Uno, Nano, Mega) have auto-reset function. At opening of USB connection the circuit around USB resets the MCU. After reset the bootloader waits a second for a new upload. If the upload doesn't happen the bootloader starts the current sketch. The serial.Serial() command in python opens the USB connection. With that the Arduino is reset and ...


2

void receiveData(int byteCount){ // Looks like your number is sent in a 3 byte packet if (bytecount == 3) { // Discard first number as it is seemingly alway zero Wire.read(); // Read low byte into rxnum int rxnum = Wire.read(); // Read high byte into rxnum rxnum += Wire.read() << 8; // Send back sendInt(rxnum);...


2

You will need PySerial to accomplish this. A tutorial on interfacing Arduino and the RPi using a usb can be found here


2

I looked at your code, but the main problem seems to be that you have not defined the problem, and it is thus not possible to definitively answer. The conventional solution to this would be to send 2 numbers with separator (usually space) followed by a newline. I know Arduino does not have an inbuilt function to do this, but you could write one. http://...


2

I guess I'll answer my own question for anyone else having this issue. What I did was: Python: # write to arduino as raw binary bge.arduino.write(struct.pack('>BBB',45,90,180)) Arduino: #include <Servo.h> Servo servo0; Servo servo1; Servo servo2; // create array int incoming[2]; void setup(){ Serial.begin(9600); servo0.attach(3); ...


2

Closely related to To know the state of USB (Serial) connection (connected or not connected) I would not be using Serial.readString() personally. How do you know where the string starts and ends? Just check for three "A" in a row. Like this: const unsigned long MESSAGE_TIMEOUT = 10UL * 1000; // 10 seconds const byte LED = 7; void setup () { Serial....


2

I think the problem is that you can't count ;) Spot the difference: 0: [0x01] Frame start 1: [0xMeterValueByte0] 2: [0xMeterValueByte1] 3: [0xMeterValueByte2] 4: [0xMeterValueByte3] 5: [0x04] Frame end and: 0: Serial.write(0x01); //start frame 1: Serial.write(0x00); 2: Serial.write(0x00); 3: Serial.write(0x02); 4: Serial.write(0x04); //end frame Your ...


2

If you are still searching for a lib check out: https://github.com/adafruit/Adafruit_Python_BluefruitLE Its specifically made for BLE communication on OS X or Linux by wrapping the corebluetooth and its Linux equivalent bluez in a python lib. Windows is not supported at this moment. Unfortunately there is no documentation to it. You will have to read the ...


2

not exactly related to Arduino but here it goes... First check if you have pip installed: pip --version if not installed go here and download the get-pip script. then on directory where you saved get-pip run on console: python get-pip.py this will install pip into your system which makes installation of packages in python way more easy. if installed ...


2

I'm pretty sure that what you are trying to do is actually call 'python setup.py' within the python script itself. Which you cannot do. The easiest way to install pyserial is via pip like so. (Windows) python -m -U pyserial --upgrade Now if you still want to use setup.py or you don't have pip installed. Make sure that you meet all of the package ...


Only top voted, non community-wiki answers of a minimum length are eligible