Pyserial with Arduino and Mu Editor

Pyserial with Arduino and Mu Editor © GPL3+

Use Mu editor for pyserial and other cool stuff!

  • 504 views
  • 0 comments
  • 5 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
Or another Arduino- compatible device.
×1

Apps and online services

About this project

In this project I am going to teach any beginner Arduino / Python programmer how to use pyserial to control your serial ports with Python code. You can't run code directly from the Python editor to your Arduino, but through the pyserial library you can send and receive messages from your device.

Step 1: Installing the Editor

Mu editor is a great editor, perfect for beginners because of its easy to use design. It also comes with a built in pyserial library, so no additional downloads are necessary. To download Mu, click here and select the correct installer for your OS. Install and then open it.

If you need the Arduino IDE, click here.

Step 2: The Arduino Part

Now just open the Arduino IDE and paste the Arduino code shown in the code section below into it and upload it to your board.

Step 3: The Python Part

Back to Mu. The first time you open it, Mu shows a "mode" window. For now select "Python 3". Later you can change this by clicking the "Mode" button in the top right corner of the screen. Later I might post projects using the "Adafruit Circuitpython" mode. Paste the Python code shown below in the code section into Mu. Now you must find the port your board is connected to. It shows this in the bottom right corner of the Arduino IDE. Type this in place of COM12. (Line 4 in the Python code.) Now run the code. Enter 1 to turn the on board LED on and 0 to turn it off.

Questions? Problems? Ideas? See comment section below. Have a great day!

Code

Arduino codeArduino
int datafromUser=0;
void setup() {
  // put your setup code here, to run once:
  pinMode( LED_BUILTIN , OUTPUT );
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0)
  {
    datafromUser=Serial.read();
  }

  if(datafromUser == '1')
  {
    digitalWrite( LED_BUILTIN , HIGH );
  }
  else if(datafromUser == '0')
  {
    digitalWrite( LED_BUILTIN, LOW);
  }
  
}
Python codePython
import serial
import time

arduino=serial.Serial('COM12', 9600)
time.sleep(2)                       #sleep 2 secs

print("Enter 1 to turn ON LED and 0 to turn OFF LED")       #print some output

while 1:                            #loop forever

    datafromUser=input()

    if datafromUser == '1':         #datafromUser equals 1?
        arduino.write(b'1')         #send '1' over serial port
        print("LED  turned ON")     #print some output

    elif datafromUser == '0':       #datafromUser equals 0?
        arduino.write(b'0')         #send '0' over serial port
        print("LED turned OFF")     #print some output

Comments

Similar projects you might like

DART EDITOR - Universal editor for Arduino MIDI projects

Project showcase by DARTMOBO

  • 1,724 views
  • 1 comment
  • 8 respects

Programming Arduino Using Python!!!

by Smart Technology

  • 22,098 views
  • 1 comment
  • 15 respects

Arduino Web Editor Secondary Features

by Arduino_Genuino

  • 8,049 views
  • 1 comment
  • 21 respects

A Chain Reaction Machine

Project showcase by Mang Pandoy

  • 2,096 views
  • 0 comments
  • 9 respects
Add projectSign up / Login