From 7d14e135bfe3cdd360c5237d2795c7d58eb5e35e Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Fri, 4 Dec 2026 08:31:19 +0000 Subject: [PATCH 1/2] Add arduino-python materials folder Sample code for the updated Arduino With Python tutorial, rewritten around pyFirmata2 2.5.1 (pyFirmata 1.1.0 fails on Python 3.11+). --- arduino-python/README.md | 45 +++++++++++++++++++++++++ arduino-python/analog_blink.py | 24 +++++++++++++ arduino-python/analog_input.py | 18 ++++++++++ arduino-python/analog_output.py | 19 +++++++++++ arduino-python/blink.py | 11 ++++++ arduino-python/digital_input.py | 17 ++++++++++ arduino-python/digital_input_compact.py | 19 +++++++++++ arduino-python/email_notification.py | 37 ++++++++++++++++++++ arduino-python/notification.py | 33 ++++++++++++++++++ arduino-python/requirements.txt | 1 + 10 files changed, 224 insertions(+) create mode 100644 arduino-python/README.md create mode 100644 arduino-python/analog_blink.py create mode 100644 arduino-python/analog_input.py create mode 100644 arduino-python/analog_output.py create mode 100644 arduino-python/blink.py create mode 100644 arduino-python/digital_input.py create mode 100644 arduino-python/digital_input_compact.py create mode 100644 arduino-python/email_notification.py create mode 100644 arduino-python/notification.py create mode 100644 arduino-python/requirements.txt diff --git a/arduino-python/README.md b/arduino-python/README.md new file mode 100644 index 0000000000..519b303c46 --- /dev/null +++ b/arduino-python/README.md @@ -0,0 +1,45 @@ +# Arduino With Python: How to Get Started + +Source files supporting the [Arduino With Python: How to Get Started](https://realpython.com/arduino-python/) tutorial on [Real Python](https://realpython.com). + +## Setup + +You need an Arduino Uno, or a compatible board, running the StandardFirmata sketch. Upload it once from the Arduino IDE by selecting *File → Examples → Firmata → StandardFirmata*. + +You should first create a virtual environment: + +```sh +$ python -m venv venv +$ source venv/bin/activate +``` + +Then install the dependencies: + +```sh +(venv) $ python -m pip install -r requirements.txt +``` + +The scripts require Python 3.11 or later and were tested with Python 3.15 and pyFirmata2 2.5.1. + +## Usage + +Every script connects to the board on `/dev/ttyACM0`. Change the port name at the top of the script to match your system, for example `"COM4"` on Windows or `"/dev/cu.usbmodem14101"` on macOS, or pass `pyfirmata2.Arduino.AUTODETECT` to let pyFirmata2 look for the board. + +| Script | Circuit | What it does | +| --- | --- | --- | +| `blink.py` | Built-in LED | Blinks the LED on pin 13 once per second | +| `digital_input.py` | Push button on pin 10, LED on pin 13 | Turns the LED on while the button is pressed | +| `digital_input_compact.py` | Same as above | Same, using `board.get_pin()` | +| `analog_input.py` | Potentiometer on A0 | Prints the analog readings | +| `analog_blink.py` | Potentiometer on A0, LED on pin 13 | Controls the blink rate with the potentiometer | +| `analog_output.py` | Potentiometer on A0, LED on pin 11 | Controls the LED brightness with PWM | +| `notification.py` | Push button on pin 10, LED on pin 13 | Shows a Tkinter message box when you press the button | +| `email_notification.py` | Push button on pin 10 | Sends an email when you press the button | + +Run a script from the command line, for example: + +```sh +(venv) $ python blink.py +``` + +Press Ctrl+C to stop a script. Before running `email_notification.py`, fill in your email addresses and an app password in `send_email()`. diff --git a/arduino-python/analog_blink.py b/arduino-python/analog_blink.py new file mode 100644 index 0000000000..b56fbeacff --- /dev/null +++ b/arduino-python/analog_blink.py @@ -0,0 +1,24 @@ +import time + +import pyfirmata2 + +board = pyfirmata2.Arduino("/dev/ttyACM0") + +analog_input = board.get_pin("a:0:i") +led = board.get_pin("d:13:o") +delay = 0.5 + + +def update_delay(analog_value): + global delay + delay = analog_value + 0.01 + + +analog_input.register_callback(update_delay) +board.samplingOn(100) + +while True: + led.write(1) + time.sleep(delay) + led.write(0) + time.sleep(delay) diff --git a/arduino-python/analog_input.py b/arduino-python/analog_input.py new file mode 100644 index 0000000000..3c79e66e54 --- /dev/null +++ b/arduino-python/analog_input.py @@ -0,0 +1,18 @@ +import time + +import pyfirmata2 + +board = pyfirmata2.Arduino("/dev/ttyACM0") + +analog_input = board.get_pin("a:0:i") + + +def print_value(analog_value): + print(analog_value) + + +analog_input.register_callback(print_value) +board.samplingOn(100) + +while True: + time.sleep(1) diff --git a/arduino-python/analog_output.py b/arduino-python/analog_output.py new file mode 100644 index 0000000000..9e7b0d4081 --- /dev/null +++ b/arduino-python/analog_output.py @@ -0,0 +1,19 @@ +import time + +import pyfirmata2 + +board = pyfirmata2.Arduino("/dev/ttyACM0") + +analog_input = board.get_pin("a:0:i") +led = board.get_pin("d:11:p") + + +def set_brightness(analog_value): + led.write(analog_value) + + +analog_input.register_callback(set_brightness) +board.samplingOn(100) + +while True: + time.sleep(1) diff --git a/arduino-python/blink.py b/arduino-python/blink.py new file mode 100644 index 0000000000..ec74734fbe --- /dev/null +++ b/arduino-python/blink.py @@ -0,0 +1,11 @@ +import time + +import pyfirmata2 + +board = pyfirmata2.Arduino("/dev/ttyACM0") + +while True: + board.digital[13].write(1) + time.sleep(1) + board.digital[13].write(0) + time.sleep(1) diff --git a/arduino-python/digital_input.py b/arduino-python/digital_input.py new file mode 100644 index 0000000000..7b30caa62f --- /dev/null +++ b/arduino-python/digital_input.py @@ -0,0 +1,17 @@ +import time + +import pyfirmata2 + +board = pyfirmata2.Arduino("/dev/ttyACM0") + + +def on_button_change(is_pressed): + board.digital[13].write(is_pressed) + + +board.digital[10].mode = pyfirmata2.INPUT +board.digital[10].register_callback(on_button_change) +board.samplingOn() + +while True: + time.sleep(1) diff --git a/arduino-python/digital_input_compact.py b/arduino-python/digital_input_compact.py new file mode 100644 index 0000000000..d9b6c87967 --- /dev/null +++ b/arduino-python/digital_input_compact.py @@ -0,0 +1,19 @@ +import time + +import pyfirmata2 + +board = pyfirmata2.Arduino("/dev/ttyACM0") + +button = board.get_pin("d:10:i") +led = board.get_pin("d:13:o") + + +def on_button_change(is_pressed): + led.write(is_pressed) + + +button.register_callback(on_button_change) +board.samplingOn() + +while True: + time.sleep(1) diff --git a/arduino-python/email_notification.py b/arduino-python/email_notification.py new file mode 100644 index 0000000000..703306a835 --- /dev/null +++ b/arduino-python/email_notification.py @@ -0,0 +1,37 @@ +import smtplib +import ssl +import time + +import pyfirmata2 + + +def send_email(): + port = 465 # For SSL + smtp_server = "smtp.gmail.com" + sender_email = "" + receiver_email = "" + password = "" + message = """Subject: Arduino Notification\n The switch was turned on.""" + + context = ssl.create_default_context() + with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: + print("Sending email") + server.login(sender_email, password) + server.sendmail(sender_email, receiver_email, message) + + +board = pyfirmata2.Arduino("/dev/ttyACM0") + +button = board.get_pin("d:10:i") + + +def on_button_change(is_pressed): + if is_pressed: + send_email() + + +button.register_callback(on_button_change) +board.samplingOn() + +while True: + time.sleep(1) diff --git a/arduino-python/notification.py b/arduino-python/notification.py new file mode 100644 index 0000000000..20aa95eb11 --- /dev/null +++ b/arduino-python/notification.py @@ -0,0 +1,33 @@ +import time +import tkinter +from tkinter import messagebox + +import pyfirmata2 + +root = tkinter.Tk() +root.withdraw() + +board = pyfirmata2.Arduino("/dev/ttyACM0") + +button = board.get_pin("d:10:i") +led = board.get_pin("d:13:o") +button_pressed = False + + +def on_button_change(is_pressed): + global button_pressed + if is_pressed: + button_pressed = True + + +button.register_callback(on_button_change) +board.samplingOn() + +while True: + if button_pressed: + button_pressed = False + led.write(1) + messagebox.showinfo("Notification", "Button was pressed") + root.update() + led.write(0) + time.sleep(0.1) diff --git a/arduino-python/requirements.txt b/arduino-python/requirements.txt new file mode 100644 index 0000000000..258a03e2ca --- /dev/null +++ b/arduino-python/requirements.txt @@ -0,0 +1 @@ +pyfirmata2==2.5.1 From 215279c247c3cff37543e6f3cb3e481da41515d6 Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Fri, 4 Dec 2026 08:33:11 +0000 Subject: [PATCH 2/2] Separate the email header from the body with a blank line --- arduino-python/email_notification.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino-python/email_notification.py b/arduino-python/email_notification.py index 703306a835..e02adf0788 100644 --- a/arduino-python/email_notification.py +++ b/arduino-python/email_notification.py @@ -11,7 +11,7 @@ def send_email(): sender_email = "" receiver_email = "" password = "" - message = """Subject: Arduino Notification\n The switch was turned on.""" + message = "Subject: Arduino Notification\n\nThe switch was turned on." context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: