Rob Braggaar
Published © CC BY-SA

LoPy - Arduino Serial Communication

Serial communication between the Pycom LoPy and the Arduino Uno.

BeginnerProtip1 hour7,740
LoPy - Arduino Serial Communication

Things used in this project

Story

Read more

Schematics

Hookup guide

Code

Lopy: boot.py

Python
Upload this script to the /flash directory on the Lopy.
"""Setup serial communication between Lopy, Arduino and PC.
Disable LED.
"""
import os
import machine
import pycom


# Configure first UART bus to see the communication on the pc
uart = machine.UART(0, 115200)
os.dupterm(uart)

# Configure second UART bus on pins P3(TX1) and P4(RX1)
uart1 = machine.UART(1, baudrate=9600)

pycom.heartbeat(False)

machine.main('main.py')
print('==========Starting main.py==========\n')

Lopy: main.py

Python
Upload this script to the /flash directory on the Lopy.
"""Sends a string to the Arduino. Waits one second and then reads the answer
from the Arduino.
"""
import time


print('Main start')

while True:
    uart1.write('Hello Arduino')
    time.sleep(1)
    print(uart1.readline())  # read the response from the Arduino
    time.sleep(1)

Arduino: serialCom.ino

Arduino
Upload this script to your Arduino Uno.
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0){ // at least one byte is available from the Lopy
    // read the message from the Lopy
    String msg = Serial.readStringUntil('\n');
    // and repeat the message back
    Serial.println("Lopy, did you say: " + msg + "?");
  }
}

Credits

Rob Braggaar

Rob Braggaar

3 projects • 13 followers
Professional tinkerer, Node specialist and tech geek

Comments