World of Electronics and Cyber Consultancy

Control Your Arduino with Python and Tkinter: Step by Step Tutorial

Welcome to an exciting journey into the world of Arduino and Python programming! In this comprehensive tutorial, we will explore the intricacies of controlling an Arduino board using Python and Tkinter, a popular GUI toolkit. Our goal is to teach you how to create a simple yet powerful user interface (UI) that allows seamless communication between your Python script and the Arduino through serial communication.

Throughout this tutorial, we will delve into the fundamentals of both Python and Arduino programming, focusing on how to set up and write code for each platform to establish a successful connection. We will also guide you through the process of building a user-friendly Tkinter interface, enabling you to send and receive data to and from the Arduino board effortlessly.

By the end of this tutorial, you will have developed a solid understanding of the concepts and techniques required to control your Arduino from a Python script using Tkinter. This knowledge will not only enhance your programming skills but also empower you to create more sophisticated projects that leverage the power of both Python and Arduino.

So, buckle up and get ready to embark on an enriching adventure into the realm of Arduino and Python programming, where the possibilities are endless, and your imagination is the only limit!

(We wrote the introduction using AI :P)

Step 1: Python Script

Import necessary modules:

  • tkinter: Provides a set of tools to create graphical user interfaces (GUIs) in Python.
  • serial: Enables communication with serial devices, such as Arduino boards.
  • time: Used to add delays in the program.
  • threading: Allows the creation of multiple threads to manage concurrent tasks.

Initialize the serial connection with the Arduino:

arduino = serial.Serial(port=’/dev/cu.usbserial-10′, baudrate=115200, timeout=.1): This line sets up the serial communication with the Arduino, using the specified port, baud rate, and timeout.

Create the main window (root) for the Tkinter GUI

root = Tk(): This creates the main window for the application.

Define a function to turn on the LED connected to the Arduino

LED(): Writes ‘1’ to the serial port, waits for 0.07 seconds, and then calls the updateState() function to update the GUI.

Define a function to turn on the buzzer connected to the Arduino

BUZZER(): Writes ‘2’ to the serial port, waits for 0.07 seconds, and then calls the updateState() function to update the GUI.

Define a function to update the GUI with the current state of the Arduino

updateState(): Reads data from the serial port, decodes it from bytes to a string, and removes any trailing characters. It then updates the text of the label in the GUI with the received data.

Define a function to refresh the GUI with the current state of the Arduino

refresh(): Writes ‘3’ to the serial port, waits for 0.07 seconds, and then calls the updateState() function to update the GUI.

Create and pack the LED button

  • led = Button(root, text=”LED”, command=LED): Creates a button with the text “LED” and assigns the LED function to its command attribute.
  • led.pack(): Adds the button to the GUI.

Create and pack the buzzer button

  • buzzer = Button(root, text=”BUZZER”, command=BUZZER): Creates a button with the text “BUZZER” and assigns the BUZZER function to its command attribute.
  • buzzer.pack(): Adds the button to the GUI.

Create and pack the refresh button

  • refresh = Button(root, text=”refresh”, command=refresh): Creates a button with the text “refresh” and assigns the refresh function to its command attribute.
  • refresh.pack(): Adds the button to the GUI.

Create and pack the label to display the Arduino’s state

  • label = Label(…): Defines a Label widget with various attributes like text, background color, font, cursor, and more.
  • label.pack(): Adds the label to the GUI.

Run the main loop of the Tkinter GUI

root.mainloop(): Starts the event loop of the GUI, allowing it to respond to user interactions.

Step 2: Arduino Sketch

This Arduino sketch sets up communication between the Arduino and a Python script running on a computer. The sketch defines two outputs (LED and buzzer) and one input (pushbutton) and establishes serial communication between the Arduino and the computer.

#define

#define led_pin 7: Defines the digital pin for the LED output.
#define buzzer_pin 8: Defines the digital pin for the buzzer output.
#define pb_pin 2: Defines the digital pin for the pushbutton input.

Create a variable x

int x;: Declares an integer variable x to store the command received from the Python script.

Void Setup Function

void setup(): The setup function is executed once when the Arduino starts.

Serial.begin(115200);: Sets the baud rate for serial communication.

Serial.setTimeout(1);: Sets the timeout for serial communication to 1 millisecond.

pinMode(led_pin, OUTPUT);: Sets the LED pin as an output.

pinMode(buzzer_pin, OUTPUT);: Sets the buzzer pin as an output.

digitalWrite(led_pin, LOW);: Sets the LED pin to LOW (off).

digitalWrite(buzzer_pin, LOW);: Sets the buzzer pin to LOW (off).

digitalWrite(pb_pin, INPUT);: Sets the pushbutton pin as an input.

attachInterrupt(digitalPinToInterrupt(pb_pin), ledstate, RISING);: Attaches an interrupt to the pushbutton pin, calling the ledstate function when the button is pressed (RISING edge).

Void Loop – Part 1

void loop(): The loop function is executed repeatedly after the setup function.
while (!Serial.available());: Waits for data to be available on the serial port.

Void Loop – Part 2

x = Serial.readString().toInt();: Reads the command from the serial port and converts it to an integer, storing it in the x variable.

switch(x): A switch statement to execute different actions based on the value of x.

case 1:: If x is equal to 1, this case block will execute.
digitalWrite(led_pin, !digitalRead(led_pin));: Toggles the LED.
delay(100);: Delays for 100 milliseconds.
updateState();: Calls the updateState() function to update the state of the LED and buzzer.
break;: Exits the switch statement.

case 2:: If x is equal to 2, this case block will execute.
digitalWrite(buzzer_pin, !digitalRead(buzzer_pin));: Toggles the buzzer.
delay(100);: Delays for 100 milliseconds.
updateState();: Calls the updateState() function to update the state of the LED and buzzer.
break;: Exits the switch statement.

case 3:: If x is equal to 3, this case block will execute.
delay(100);: Delays for 100 milliseconds.
updateState();: Calls the updateState() function to update the state of the LED and buzzer.
break;: Exits the switch statement.

default:: If x is not equal to 1, 2, or 3, this case block will execute.
delay(100);: Delays for 100 milliseconds.
updateState();: Calls the updateState() function to update the state of the LED and buzzer.
break;: Exits the switch statement.

Vois updateState Function

void updateState(): This function updates the state of the LED and buzzer by reading their current states and concatenating a string with the current state of the LED and buzzer.

Serial.println(m);: Sends the string m containing the current state of the LED and buzzer to the serial port.

Void ledstate() Function

void ledstate(): This function toggles the LED when the pushbutton is pressed.

Result: