Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions arduino-python/README.md
Original file line number Diff line number Diff line change
@@ -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()`.
24 changes: 24 additions & 0 deletions arduino-python/analog_blink.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions arduino-python/analog_input.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions arduino-python/analog_output.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions arduino-python/blink.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions arduino-python/digital_input.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions arduino-python/digital_input_compact.py
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 37 additions & 0 deletions arduino-python/email_notification.py
Original file line number Diff line number Diff line change
@@ -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 = "<your email address>"
receiver_email = "<destination email address>"
password = "<app password>"
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:
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)
33 changes: 33 additions & 0 deletions arduino-python/notification.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions arduino-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pyfirmata2==2.5.1
Loading