Python's built-in time module is also imported as the time.sleep() function will be used in the script. Note that even though the package is called PySerial, the line import serial is used. Ensure PySerial is installed before running the Python script.Īt the top of the Python script, import the PySerial package. Again, the serial communication between the Python script and the Arduino is facilitated by the PySerial package. Write a Python Script to turn the LED on and offĪfter the LED turns on and off based on sending H and L with the Arduino Serial Monitor and you can turn the LED on and off with the Python REPL, it's time to write a Python script to turn the LED on and off. > ser = serial.Serial('COM4', 9600) # open serial portĮnsure the command ser.close() is issued before exiting the Python REPL. When you type the command: ser = serial.Serial('COM4', 9800, timeout=1), ensure the 'COM#' corresponds to the port that worked with the Arduino IDE in Tools -> Port. If the line does not start with a REPL prompt, the line represents expected output. If a REPL prompt > precedes the command, type the command into the REPL. (You can open the Python REPL by typing > python at the Anaconda Prompt) At the Python REPL, type the following commands. Next, use the Python REPL to turn the Arduino on and off. In the Serial Monitor type L or H and click. Open the Arduino Serial Monitor using Tools -> Serial Monitor. If the sketch does not upload, check which COM port is selected in Tools -> Port. In the Arduino IDE, click the to verify and the to upload. Make sure the Board is selected in the Arduino IDE under Tools -> Board -> Arduino/Genuino Uno.Make sure the Port is selected properly in the Arduino IDE under Tools -> Port.Ensure two settings in the Arduino IDE are set correctly. if it's an L (ASCII 76) turn off the LED:Ĭonnect the Arduino to the computer using a USB cable. if it's a capital H (ASCII 72), turn on the LED: read the oldest byte in the serial buffer: Int incomingByte // variable stores serial data File -> Examples -> 04.Communication -> PhysicalPixelĬonst int ledPin = 13 // pin the LED is attached to
The code for the Physical Pixel Sketch is shown below. The Physical Pixel sketch is found in the Arduino IDE under File -> Examples -> 04.Communication -> PhysicalPixel The code is same as in the example sketch called Physical Pixel. Upload the following code to the Arduino using the Arduino IDE. This type of resistor is called a pull up resistor. A resistor is needed to prevent too much current from flowing through the LED.
Note the short leg of the LED is connected to ground, and the long leg of the resistor is connected through a resistor to PIN 13. Ĭonnect the LED to the Arduino using a resistor, wires and a breadboard.
If you are working on a computer that you don't have administrator privileges to install software on, be sure to select.
Download the Arduino IDE using the following link: You will also need to download the Arduino IDE (Integrated Development Environment).
In this section, you will learn how to control an LED connected to an external piece of hardware (an Arduino) using Python. Controlling an LED with Python Controlling an LED with Python Problem Solving with Python Book Construction Write a Python script to allow a user to turn the LED on and off Write a Python Script to turn the LED on and off Use the Python REPL to turn the Arduino LED on and off Turn the LED on and off with the Arduino Serial Monitor