AJ-Explains-It-All
Published © GPL3+

Controlling an Arduino Using Python Shell

One of the many ways to tinker with your board!

BeginnerFull instructions provided1 hour2,881
Controlling an Arduino Using Python Shell

Things used in this project

Story

Read more

Code

Ard123

Python
import serial #Serial imported for Serial communication
import time #Required to use delay functions

ArduinoSerial = serial.Serial(com18,9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 secounds for the communication to get established

print ArduinoSerial.readline() #read the serial data and print it as line
print (Enter 1 to turn ON LED and 0 to turn OFF LED)
while 1: #Do this forever

var = raw_input() #get input from user
print you entered, var #print the intput for confirmation

if (var == 1): #if the value is 1
ArduinoSerial.write(1) #send 1
print (LED turned ON)
time.sleep(1)

if (var == 0): #if the value is 0
ArduinoSerial.write(0) #send 0
print (LED turned OFF)
time.sleep(1)

Py23

Arduino
void loop()
{
while (Serial.available())
{
data = Serial.read();
}
if (data == ‘1’)
digitalWrite (LED_BUILTIN, HIGH);
else if (data == ‘0’)
digitalWrite (LED_BUILTIN, LOW);
}

Py12

Arduino
void setup()
{
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(LED_BUILTIN, OUTPUT); //make the LED pin (13) as output
digitalWrite (LED_BUILTIN, LOW);
Serial.println(“Hi!, I am Arduino”);
}

Credits

AJ-Explains-It-All
12 projects • 10 followers
Experienced embedded firmware and software developer. I have a great affinity towards ARM cortex based MCU's.

Comments