Getting Started with the Arduino UNO Q

A beginner's guide to programming hardware with C++ and Python.

Part 1: The C++ Sketch (Controlling the MCU)

An Arduino "sketch" is the name for a program written for the microcontroller. It's written in C++. Every sketch has two main functions:

Your First Sketch: Blinking an LED

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).

Goal: Make the onboard LED (connected to pin 13) turn on for one second, then off for one second, repeatedly.
// 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.
}

Part 2: Python on the MPU (The Linux Brain)

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.

Project: Control the LED from Python

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!

Step 1: The C++ Sketch to Listen for Commands

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);
}

Step 2: The Python Script to Send Commands

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
What's Happening?
The Python script is the boss. It tells the C++ sketch when to run its 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.