A beginner's guide to programming hardware with C++ and Python.
An Arduino "sketch" is the name for a program written for the microcontroller. It's written in C++. Every sketch has two main functions:
void setup() { ... }: This code runs once when the board first powers on. You use it for initialization, like telling the board which pins you're going to use.void loop() { ... }: This code runs over and over again, forever. This is where you put the main logic of your program, like blinking an LED or reading a sensor.This is the "Hello, World!" of hardware. It will blink the built-in LED on the board. This code is written in the C++ sketch file (e.g., sketch.ino).
// This code runs on the MCU (the real-time brain) const int LED_PIN = 13; // The built-in LED is on pin 13 void setup() { // This function runs once at the start. pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output. } void loop() { // This function runs in a loop, forever. digitalWrite(LED_PIN, HIGH); // Turn the LED on. HIGH is voltage ON. delay(1000); // Wait for 1000 milliseconds (1 second). digitalWrite(LED_PIN, LOW); // Turn the LED off. LOW is voltage OFF. delay(1000); // Wait for another second. }
The MPU side runs Python. From here, you can't directly control the hardware pins. Instead, you use the Arduino Bridge library to talk to the C++ sketch running on the MCU.
This allows you to separate your program's logic. Python can handle the complex stuff (like timing or web requests), while the C++ sketch focuses purely on the hardware task it's told to do.
Let's modify the sketch so it waits for a command from Python before blinking the LED. This is a much more powerful way to program!
This sketch includes the Bridge.h library. It defines a "handler" named "blink" that Python can call.
// C++ Sketch: sketch.ino #include <Bridge.h> const int LED_PIN = 13; void setup() { pinMode(LED_PIN, OUTPUT); Bridge.begin(); // Initialize the Bridge // Register a function named 'blinkHandler' to be called // when Python sends the "blink" command. Bridge.on("blink", blinkHandler); } void loop() { // The Bridge needs to check for new messages from Python. Bridge.process(); } // This function is called by the Bridge when Python requests it. void blinkHandler() { digitalWrite(LED_PIN, HIGH); delay(500); digitalWrite(LED_PIN, LOW); }
This Python script runs on the Linux side. It connects to the Bridge and calls the "blink" command every two seconds.
# Python Script: main.py from arduinoio import Bridge import time # Initialize the bridge connection to the MCU bridge = Bridge() bridge.begin() print("Python script started. Sending 'blink' command every 2 seconds...") while True: # Call the handler named "blink" that we registered in the C++ sketch bridge.call("blink") print("Sent 'blink' command to MCU.") time.sleep(2) # Wait for 2 seconds
blinkHandler function. This "dual-brain" approach is the key to building powerful projects with the Arduino UNO Q. The C++ sketch handles the low-level hardware work, and the Python script handles the high-level logic and timing.