Arduino and Python / Matlab

The goal

We want to communicate with an Arduino from Python / Matlab.

Questions to David Rotermund

Get the environment ready

Before we can really interact with the Arduino board, we need to make the environment ready first:

Download and install the Arduino IDE. Connect the Arduino via USB cable to the computer. Open the Arduino IDE. Open the Blink example. Files -> Examples -> 01. Basics -> Blink Check the setting: Tools-> Board and Tools->Port Get the board information as a test: Tools -> Get Board Info Upload the sketch (means source code)

image0

The Arduino led should blink now.

If you use Python for controlling the Arduino, make sure that the serial package is installed:

pip install pyserial

WARNING: If you open a communication channel with Python / Matlab then a reset signal is created that resets the Arduino. Thus only open the communication once and keep it open as long as you require a connection to the Arduino.

In the case that you are a VS code user, you want to add the Microsoft Arduino extension to VS code for editing the Arduino ino files in style and not like a caveperson with the Arduino IDE:

image1

Controlling the LED

We want to control the LED on the Arduino board via an external software.

Note: I see it as a good style to make sure that we communicate in a fashion that reduces misunderstandings. Thus I will start any control sequence with the integer value 60 (the < symbol in ASCII) and end it with the integer value 62 (the > symbol in ASCII). The source code is prepared to handle up to 32x 8 bit integer values as payload. However we will only evaluate the value at index 0 in the switch case construct.

The Arduino sketch

int rc;
boolean recvInProgress = false;

int startMarker = 60; // <
int endMarker = 62; // >

int numChars = 32;
int receivedChars[32];

int index = 0;
int Used = 0;

boolean newData = false;

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  
}

void loop() {

  if ((Serial.available() > 0) && (newData == false)) {

    rc = Serial.read();

    if ((recvInProgress == true) && (rc == endMarker)){
      recvInProgress = false;
      newData = true;
    }
    else if(recvInProgress == true) {
      receivedChars[Used] = rc;
      Used++;
      if (Used >= numChars) {
        Used = numChars - 1;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
      Used = 0;
    }
  }

  if (newData == true){
    for (index = 0; index < Used; index++) {
      
      switch (index) {
        case 0:
          if (int(receivedChars[index]) == 1){
            digitalWrite(LED_BUILTIN, HIGH);
          }
          else{
            digitalWrite(LED_BUILTIN, LOW);
          }
          break;
        default:
          break;
      }
    }
    
    newData = false;
  }
  
}

Python

import numpy as np
import time
import serial

time_in_sec_until_change: float = 0.5

ser = serial.Serial("COM3", 115200)
# need to wait until the arduino is ready...
# I tested with 1sec and it was not enough
time.sleep(2)


start_marker: int = 60  # '<'
stop_marker: int = 62  # '>'
led_value_off: np.ndarray = np.array((start_marker, 0, stop_marker), dtype=np.uint8)
led_value_on: np.ndarray = np.array((start_marker, 1, stop_marker), dtype=np.uint8)

for t in range(0, 100):
    print("On")
    ser.write(led_value_on)
    print("Wait")
    time.sleep(time_in_sec_until_change)

    print("Off")
    ser.write(led_value_off)
    print("Wait")
    time.sleep(time_in_sec_until_change)

print("Done")

Matlab

time_in_sec_until_change = 0.5;

ser = serialport('COM3', 115200); 
% need to wait until the arduino is ready...
% I tested with 1sec and it was not enough
pause(2); 

start_marker = uint8(60);  % '<'
stop_marker = uint8(62);  % '>'

led_value_off = [start_marker, uint8(0), stop_marker];
led_value_on = [start_marker, uint8(1), stop_marker];

for i = [1:1:100]
    fprintf("On\n")
    write(ser,led_value_on,"uint8")
    fprintf("Wait\n")
    pause(time_in_sec_until_change)

    fprintf("Off\n")
    write(ser,led_value_off,"uint8")
    fprintf("Wait\n")
    pause(time_in_sec_until_change)
end
fprintf("Done\n")

clear ser 

Pulsing the LED for a defined time

The Arduino sketch

int rc;
boolean recvInProgress = false;

int startMarker = 60; // <
int endMarker = 62; // >

int numChars = 32;
int receivedChars[32];

int index = 0;
int Used = 0;

boolean newData = false;

unsigned long my_timeout = 5000; // in ms
unsigned long my_timer = my_timeout; // counter
unsigned long last_time_check = 0;
unsigned long this_time_check = 0;

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  
}

void loop() {

  if (my_timer >= my_timeout){
    digitalWrite(LED_BUILTIN, LOW);
   }
  else{
    this_time_check = millis();
    my_timer = my_timer + this_time_check - last_time_check;
    last_time_check = this_time_check;
  }

  if ((Serial.available() > 0) && (newData == false)) {

    rc = Serial.read();

    if ((recvInProgress == true) && (rc == endMarker)){
      recvInProgress = false;
      newData = true;
    }
    else if(recvInProgress == true) {
      receivedChars[Used] = rc;
      Used++;
      if (Used >= numChars) {
        Used = numChars - 1;
      }
    }
    else if (rc == startMarker) {
      recvInProgress = true;
      Used = 0;
    }
  }

  if (newData == true){
    for (index = 0; index < Used; index++) {
      
      switch (index) {
        case 0:
          if (int(receivedChars[index]) == 1){
            digitalWrite(LED_BUILTIN, HIGH);
            my_timer = 0;
            last_time_check = millis();
          }
          break;
        default:
          break;
      }
    }
    
    newData = false;
  }
  
}

Python

import numpy as np
import time
import serial

ser = serial.Serial("COM3", 115200)
# need to wait until the arduino is ready...
# I tested with 1sec and it was not enough
time.sleep(2)


start_marker: int = 60  # '<'
stop_marker: int = 62  # '>'
led_value_on: np.ndarray = np.array((start_marker, 1, stop_marker), dtype=np.uint8)

print("On")
ser.write(led_value_on)

Matlab

ser = serialport('COM3', 115200); 
% need to wait until the arduino is ready...
% I tested with 1sec and it was not enough
pause(2); 

start_marker = uint8(60);  % '<'
stop_marker = uint8(62);  % '>'

led_value_on = [start_marker, uint8(1), stop_marker];

fprintf("On\n")
write(ser,led_value_on,"uint8")

clear ser 

PWM control of a pin

Warning: On an Arduino Uno LED_BUILTIN is 13. But pins that support PWM on an Uno are 3, 5, 6, 9, 10, 11 and NOT 13.

With a output value of 64:

image2

With a output value of 128:

image3

With a output value of 192:

image4

The Arduino sketch

int rc;
boolean recvInProgress = false;

int startMarker = 60; // <
int endMarker = 62; // >

int numChars = 32;
int receivedChars[32];

int index = 0;
int Used = 0;

int min_length_of_payload = 1;

boolean newData = false;

int output_pin = 11;

void setup() {
  Serial.begin(115200);
  pinMode(output_pin, OUTPUT);
  
}

void loop() {

  if ((Serial.available() > 0) && (newData == false)) {

    rc = Serial.read();

    if ((recvInProgress == true) && (rc == endMarker)  && (Used == min_length_of_payload) ){
      recvInProgress = false;
      newData = true;
    }
    else if ((recvInProgress == true) && (rc != endMarker)  && (Used == min_length_of_payload) ){
      recvInProgress = false;
      newData = false;
      Used = 0;
    }
    else if(recvInProgress == true) {
      receivedChars[Used] = rc;
      Used++;
    }
    else if (rc == startMarker) {
      recvInProgress = true;
      Used = 0;
    }
  }

  if (newData == true){
    for (index = 0; index < Used; index++) {
      
      switch (index) {
        case 0:
          // the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.
          analogWrite(output_pin, int(receivedChars[index]));
          break;
        default:
          break;
      }
    }
    
    newData = false;
  }
  
}

Python

import numpy as np
import time
import serial


ser = serial.Serial("COM3", 115200)
# need to wait until the arduino is ready...
# I tested with 1sec and it was not enough
time.sleep(2)


start_marker: int = 60  # '<'
stop_marker: int = 62  # '>'
led_brightness = 128  # 0,...,255 integer values allowed

led_control: np.ndarray = np.array(
    (start_marker, led_brightness, stop_marker), dtype=np.uint8
)
print("Send...")
ser.write(led_control)

Matlab

ser = serialport('COM3', 115200); 
% need to wait until the arduino is ready...
% I tested with 1sec and it was not enough
pause(2); 

start_marker = uint8(60);  % '<'
stop_marker = uint8(62);  % '>'

led_brightness = 128; % 0,...,255 integer values allowed

led_control = [start_marker, uint8(led_brightness),stop_marker];

fprintf('Send...')
write(ser,led_control,"uint8")

clear ser 

Sending the internal clock

The Arduino sketch

int start_marker = 60; // <
int end_marker = 62; // >

unsigned long mytime;

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

void loop() {

  mytime = millis();
  Serial.write(start_marker);

  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));

  Serial.write(end_marker);
  delay(1000);
  
}

Python

import time
import serial

read_max_values = 6
read_number_of_values = 100

ser = serial.Serial("COM3", 115200)
# need to wait until the arduino is ready...
# I tested with 1sec and it was not enough
time.sleep(2)


start_marker: int = 60  # '<'
stop_marker: int = 62  # '>'

for i in range(0, read_number_of_values):
    # I will assume that we get one block of 6 bytes
    # I want to keep this example simple...
    s = ser.read(read_max_values)
    returned_value: int = 0
    if len(s) == 6:
        if s[0] == start_marker:
            if s[5] == stop_marker:
                returned_value += s[4]
                returned_value = returned_value << 8
                returned_value += s[3]
                returned_value = returned_value << 8
                returned_value += s[2]
                returned_value = returned_value << 8
                returned_value += s[1]
                print(f"Arduino-clock in ms: {returned_value}")

Matlab

ser = serialport('COM3', 115200); 
% need to wait until the arduino is ready...
% I tested with 1sec and it was not enough
pause(2); 

start_marker = uint8(60);  % '<'
stop_marker = uint8(62);  % '>'

read_max_values = 6;
read_number_of_values = 100;

for i = [1:1:read_number_of_values],
    % I will assume that we get one block of 6 bytes
    % I want to keep this example simple...
    s = read(ser,read_max_values,"uint8");
    returned_value = 0;
    if length(s) == 6,
        if s(1) == start_marker,
            if s(6) == stop_marker,
                returned_value = returned_value + s(5);
                returned_value = bitshift(returned_value,8);
                returned_value = returned_value + s(4);
                returned_value = bitshift(returned_value,8);
                returned_value = returned_value + s(3);
                returned_value = bitshift(returned_value,8);
                returned_value = returned_value + s(2);
                fprintf(['Arduino-clock in ms: ' num2str(returned_value) '\n']);
            end
        end
    end
end
clear ser 

Sending the time on a rising interrupt

Via attachInterrupt() we attach a function to an interrupt which is triggered when a low -> high transition on a defined pin ocures. Not every pin is capable of doing so. I will use pin 2 on the Uno. Please check attachInterrupt() for list of pins.

The software side is the same as in the previous example.

The Arduino sketch

int start_marker = 60; // <
int end_marker = 62; // >

volatile unsigned long mytime;
int interruptPin = 2;

void setup() {
  Serial.begin(115200);

  attachInterrupt(digitalPinToInterrupt(interruptPin), send_time, RISING );
}
void send_time(){
  mytime = millis();
  Serial.write(start_marker);

  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));

  Serial.write(end_marker);
  }
void loop() {

  
}

Reading digital values on a rising interrupt

The Arduino sketch

int start_marker = 60; // <
int end_marker = 62; // >

volatile unsigned long mytime;
int interruptPin = 2;
int analogPin = A0;
volatile unsigned int digital_value = 0;

void setup() {
  Serial.begin(115200);

  attachInterrupt(digitalPinToInterrupt(interruptPin), send_time, RISING );
}
void send_time(){
  mytime = millis();
  digital_value = digitalRead(analogPin);
  Serial.write(start_marker);

  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));

  Serial.write(byte(digital_value));

  Serial.write(end_marker);
  }
void loop() {

  
}

Python:

import numpy as np
import time
import serial

read_max_values = 7
read_number_of_values = 100

ser = serial.Serial("COM3", 115200)
# need to wait until the arduino is ready...
# I tested with 1sec and it was not enough
time.sleep(2)

start_marker: int = 60  # '<'
stop_marker: int = 62  # '>'

buffer: np.ndarray = np.array([])

counter_samples = 0
while True:

    s = ser.read(read_max_values)
    returned_value_time: int = 0
    returned_value_bool: int = 0

    # Find the start_marker and remove everything before that
    buffer_length: int = buffer.size
    if buffer_length > 0:
        for i in range(0, buffer_length):
            if buffer[0] == start_marker:
                break
            else:
                buffer = buffer[1:]

    if buffer.shape[0] == 0:
        buffer = np.frombuffer(s, dtype=np.uint8)
    else:
        buffer = np.concatenate([buffer, np.frombuffer(s, dtype=np.uint8)], axis=0)

    if buffer.size >= 7:
        if buffer[0] == start_marker:
            if buffer[6] == stop_marker:
                returned_value_time += buffer[4]
                returned_value_time = returned_value_time << 8
                returned_value_time += buffer[3]
                returned_value_time = returned_value_time << 8
                returned_value_time += buffer[2]
                returned_value_time = returned_value_time << 8
                returned_value_time += buffer[1]

                returned_value_bool = buffer[5]

                if buffer.size == 7:
                    buffer = np.array([])
                else:
                    buffer = buffer[7:]
                counter_samples += 1
                print(
                    f"Arduino-clock in ms: {returned_value_time} with value {returned_value_bool}"
                )
    if counter_samples == read_number_of_values:
        break

Reading analog values on a rising interrupt

The Arduino sketch

int start_marker = 60; // <
int end_marker = 62; // >

volatile unsigned long mytime;
int interruptPin = 2;
int analogPin = A0;
volatile unsigned int adc_value = 0;

void setup() {
  Serial.begin(115200);

  attachInterrupt(digitalPinToInterrupt(interruptPin), send_time, RISING );
}
void send_time(){
  mytime = millis();
  adc_value = analogRead(analogPin);
  Serial.write(start_marker);

  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));
  mytime = mytime >> 8;
  Serial.write(byte(mytime));

  Serial.write(byte(adc_value));
  adc_value = adc_value >> 8;
  Serial.write(byte(adc_value));

  Serial.write(end_marker);
  }
void loop() {
  
}

Python:

import numpy as np
import time
import serial

read_max_values = 8
read_number_of_values = 100

ser = serial.Serial("COM3", 115200)
# need to wait until the arduino is ready...
# I tested with 1sec and it was not enough
time.sleep(2)

start_marker: int = 60  # '<'
stop_marker: int = 62  # '>'

buffer: np.ndarray = np.array([])

counter_samples = 0
while True:

    s = ser.read(read_max_values)
    returned_value_time: int = 0
    returned_value_adc: int = 0

    # Find the start_marker and remove everything before that
    buffer_length: int = buffer.size
    if buffer_length > 0:
        for i in range(0, buffer_length):
            if buffer[0] == start_marker:
                break
            else:
                buffer = buffer[1:]

    if buffer.shape[0] == 0:
        buffer = np.frombuffer(s, dtype=np.uint8)
    else:
        buffer = np.concatenate([buffer, np.frombuffer(s, dtype=np.uint8)], axis=0)

    if buffer.size >= 8:
        if buffer[0] == start_marker:
            if buffer[7] == stop_marker:
                returned_value_time += buffer[4]
                returned_value_time = returned_value_time << 8
                returned_value_time += buffer[3]
                returned_value_time = returned_value_time << 8
                returned_value_time += buffer[2]
                returned_value_time = returned_value_time << 8
                returned_value_time += buffer[1]

                returned_value_adc += buffer[6]
                returned_value_adc = returned_value_adc << 8
                returned_value_adc += buffer[5]

                if buffer.size == 8:
                    buffer = np.array([])
                else:
                    buffer = buffer[8:]
                counter_samples += 1
                print(
                    f"Arduino-clock in ms: {returned_value_time} with value {returned_value_adc}"
                )
    if counter_samples == read_number_of_values:
        break

Language Reference

Variabel types

 
array
bool
boolean
byte
char
double
float
int
long
short
size_t
string
String()
unsigned char
unsigned int
unsigned long
word
void

Type casts

 
(unsigned int)
(unsigned long)
byte()
char()
float()
int()
long()
word()

“math” functions**

   
abs() Calculates the absolute value of a number.
constrain() Constrains a number to be within a range.
map() Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.
max() Calculates the maximum of two numbers.
min() Calculates the minimum of two numbers.
pow() Calculates the value of a number raised to a power. pow() can be used to raise a number to a fractional power. This is useful for generating exponential mapping of values or curves.
sq() Calculates the square of a number: the number multiplied by itself.
sqrt() Calculates the square root of a number.
cos() Calculates the cosine of an angle (in radians). The result will be between -1 and 1.
sin() Calculates the sine of an angle (in radians). The result will be between -1 and 1.
tan() Calculates the tangent of an angle (in radians). The result will be between negative infinity and infinity.
random() The random function generates pseudo-random numbers.
randomSeed() randomSeed() initializes the pseudo-random number generator, causing it to start at an arbitrary point in its random sequence. This sequence, while very long, and random, is always the same.
   
bit() Computes the value of the specified bit (bit 0 is 1, bit 1 is 2, bit 2 is 4, etc.).
bitClear() Clears (writes a 0 to) a bit of a numeric variable.
bitRead() Reads a bit of a variable, e.g. bool, int. Note that float & double are not supported. You can read the bit of variables up to an unsigned long long (64 bits / 8 bytes).
bitSet() Sets (writes a 1 to) a bit of a numeric variable.
bitWrite() Writes to a bit of a variable, e.g. bool, int, long. Note that float & double are not supported. You can write to a bit of variables up to an unsigned long (32 bits / 8 bytes).
highByte() Extracts the high-order (leftmost) byte of a word (or the second lowest byte of a larger data type).
lowByte() Extracts the low-order (rightmost) byte of a variable (e.g. a word).

Control structures:

 
for
do…while
while
break
continue
if
else
switch…case
return
goto

Further Syntax

 
#define (define)
#include (include)
/* */ (block comment)
// (single line comment)
; (semicolon)
{} (curly braces)

Arithmetic Operators

 
% (remainder)
* (multiplication)
+ (addition)
- (subtraction)
/ (division)
= (assignment operator)

Comparison Operators

 
!= (not equal to)
< (less than)
<= (less than or equal to)
== (equal to)
> (greater than)
>= (greater than or equal to)

Boolean Operators

 
! (logical not)
&& (logical and)
|| (logical or)

Pointer Access Operators

 
& (reference operator)
* (dereference operator)

Bitwise Operators

 
& (bitwise and)
« (bitshift left)
» (bitshift right)
^ (bitwise xor)
| (bitwise or)
~ (bitwise not)

Compound Operators

 
%= (compound remainder)
&= (compound bitwise and)
*= (compound multiplication)
++ (increment)
+= (compound addition)
– (decrement)
-= (compound subtraction)
/= (compound division)
^= (compound bitwise xor)
|= (compound bitwise or)

Structure

   
setup() “The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup() function will only run once, after each powerup or reset of the Arduino board.”
loop() “After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. “

Digital Pins

   
pinMode() “Configures the specified pin to behave either as an input or an output.”
digitalRead() “Reads the value from a specified digital pin, either HIGH or LOW.”
digitalWrite() “Write a HIGH or a LOW value to a digital pin.”

Analog Pins

   
analogRead() “Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023.”
analogWrite() “Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin.”

Time

   
millis() “Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.”
micros() “Returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes.”
delay() “Pauses the program for the amount of time (in milliseconds) specified as parameter.”
delayMicroseconds() “Pauses the program for the amount of time (in microseconds) specified by the parameter. […] Currently, the largest value that will produce an accurate delay is 16383; larger values can produce an extremely short delay.”

Map

   
map() “Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between”

Serial

   
Serial.begin() “Sets the data rate in bits per second (baud) for serial data transmission.” WARNING: What ever you use as a data rate, make sure that the data rate is set to the same value everywhere!
if(Serial) “Indicates if the specified Serial port is ready. On the boards with native USB, if (Serial) (or if(SerialUSB) on the Due) indicates whether or not the USB CDC serial connection is open. For all other boards, and the non-USB CDC ports, this will always return true.”

Read

   
Serial.available() “Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes).”
Serial.read() “Reads incoming serial data.” Note: Reads ONE byte into a int variable.
Serial.readBytes() “Serial.readBytes() reads characters from the serial port into a buffer. The function terminates if the determined length has been read, or it times out (see Serial.setTimeout()).”
Serial.setTimeout() “Serial.setTimeout() sets the maximum milliseconds to wait for serial data. It defaults to 1000 milliseconds.”
Serial.peek() “Returns the next byte (character) of incoming serial data without removing it from the internal serial buffer. That is, successive calls to peek() will return the same character, as will the next call to read().”

Write

   
Serial.availableForWrite() “Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation.”
Serial.write() “Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.”
Serial.print() “Prints data to the serial port as human-readable ASCII text. “
Serial.println() “Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ‘\n’). This command takes the same forms as Serial.print().”
Serial.flush() “Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)”

Interrupts

   
attachInterrupt() Connect a function to one of the interrupt pins
detachInterrupt() “Turns off the given interrupt.”
interrupts() “Re-enables interrupts (after they’ve been disabled by noInterrupts(). “
noInterrupts() “Disables interrupts (you can re-enable them with interrupts()).”

Constants

 
Floating Point Constants
HIGH
LOW
INPUT
INPUT_PULLUP
OUTPUT
Integer Constants
LED_BUILTIN
true
false

Communication

 
Print
Serial
SPI
Stream
Wire

USB

 
Keyboard
Mouse

The source code is Open Source and can be found on GitHub.