Skip to content

Xiao RP2040 Arduino-Pico

alt text

alt text

Ref. Installation

  1. Open up the Arduino IDE and go to File->Preferences.
  2. In the dialog that pops up, enter the following URL in the “Additional Boards Manager URLs” https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
  3. Hit OK to close the dialog.
  4. Go to Tools->Boards->Board Manager in the IDE
  5. Type “pico” in the search box and select “Add”:

alt text

///
/// Work
///
const int ledPin = 26;  // Pin where the LED is connected P26/A0/D0

void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn the LED on
  delay(1000);                 // Wait for 1 second
  digitalWrite(ledPin, LOW);   // Turn the LED off
  delay(1000);                 // Wait for 1 second
}
///
/// Error
///
const int ledPin = GP26;  // Pin where the LED is connected P26/A0/D0

void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn the LED on
  delay(1000);                 // Wait for 1 second
  digitalWrite(ledPin, LOW);   // Turn the LED off
  delay(1000);                 // Wait for 1 second
}
///
/// Error
///
const int ledPin = P26;  // Pin where the LED is connected P26/A0/D0

void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn the LED on
  delay(1000);                 // Wait for 1 second
  digitalWrite(ledPin, LOW);   // Turn the LED off
  delay(1000);                 // Wait for 1 second
}
///
/// Work
///
const int ledPin = D0;  // Pin where the LED is connected P26/A0/D0

void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn the LED on
  delay(1000);                 // Wait for 1 second
  digitalWrite(ledPin, LOW);   // Turn the LED off
  delay(1000);                 // Wait for 1 second
}
///
/// Work
///
const int ledPin1 = 26;  // Pin where the LED is connected P26/A0/D0
const int ledPin2 = D1;  // Pin where the LED is connected P27/A1/D1

void setup() {
  pinMode(ledPin1, OUTPUT);  // Set the LED pin as an output
  pinMode(ledPin2, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  digitalWrite(ledPin1, HIGH);  // Turn the LED on
  digitalWrite(ledPin2, HIGH);  // Turn the LED on
  delay(1000);                 // Wait for 1 second
  digitalWrite(ledPin1, LOW);   // Wait for 1 second
  digitalWrite(ledPin2, LOW);   // Wait for 1 second
  delay(1000);                 // Wait for 1 second
}
///
/// Work
///
const int ledPin = 0;  // Pin where the LED is connected P0/TX/D6

void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  digitalWrite(ledPin, HIGH);  // Turn the LED on
  delay(1000);                 // Wait for 1 second
  digitalWrite(ledPin, LOW);   // Turn the LED off
  delay(1000);                 // Wait for 1 second
}

DAC(D/A Converter): Analog Output

DAC (Digital-to-Analog Converter)

Xiao RP2040 doesn’t have a hardware DAC, so analogWrite() generates a PWM-based simulated analog signal.

analogWright()

analogWrite() generates a PWM signal that mimics an analog output on microcontrollers without a DAC (e.g., RP2040).
analogWrite() outputs a true analog voltage on microcontrollers with a DAC (e.g., ESP32)

analogWrite() works only on PWM-supported pins in Arduino. However, Xiao RP2040 can use PWM on any digital pin, so analogWrite() works on all pins on this board.

///
///Work
///
int ledPin = 26;  // LED connected to pin P26/A0/D0

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}
///
///Work
///
int ledPin = A0;  // LED connected to pin P26/A0/D0

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}
///
///Work
///
int ledPin = D0;  // LED connected to pin P26/A0/D0

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}
///
///Work
///
int ledPin = D4;  // LED connected to pin P6/SDA/D4

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

ADC(A/D Converter): Analog Input

ADC (Analog-to-Digital Converter)

Xiao RP2040’s A0, A1, A2, and A3 are pins that support Analog Input.

///
///Work
///
/// Connect A0 to GND, Serial Monitor shows value = 0
/// Connect A0 to 3.3V, Serial Monitor shows value = 1023
///
int analogPin = A0;  //  Pin where the analogRead is connected P26/A0/D0

void setup() {
    Serial.begin(115200);  // Start serial communication at 115200 baud rate

}

void loop() {
    int sensorValue = analogRead(analogPin);  // Reads the voltage from pin A0 (0V to 3.3V) and converts it into a 12-bit value (0-4095)

    Serial.println(sensorValue);  // Print the value to the Serial Monitor
    delay(500);  // Wait 500 milliseconds before the next reading
}
///
///Work
///
/// Connect A0 to GND, Serial Monitor shows value = 0
/// Connect A0 to 3.3V, Serial Monitor shows value = 4095
///
int analogPin = A0;  //  Pin where the analogRead is connected P26/A0/D0

void setup() {
    Serial.begin(115200);  // Start serial communication at 115200 baud rate
    analogReadResolution(12);  // Set ADC resolution to 12-bit (0-4095)
}

void loop() {
    int sensorValue = analogRead(analogPin);  // Reads the voltage from pin A0 (0V to 3.3V) and converts it into a 12-bit value (0-4095)

    Serial.println(sensorValue);  // Print the value to the Serial Monitor
    delay(500);  // Wait 500 milliseconds before the next reading
}
///
///Work
///
/// Connect A0 to GND, Serial Monitor shows value = 0
/// Connect A0 to 3.3V, Serial Monitor shows value = 4095
///
int analogPin = 26;  //  Pin where the analogRead is connected P26/A0/D0

void setup() {
    Serial.begin(115200);  // Start serial communication at 115200 baud rate
    analogReadResolution(12);  // Set ADC resolution to 12-bit (0-4095)
}

void loop() {
    int sensorValue = analogRead(analogPin);  // Reads the voltage from pin A0 (0V to 3.3V) and converts it into a 12-bit value (0-4095)

    Serial.println(sensorValue);  // Print the value to the Serial Monitor
    delay(500);  // Wait 500 milliseconds before the next reading
}

///
///Work
///
/// Connect A0 to GND, Serial Monitor shows value = 0
/// Connect A0 to 3.3V, Serial Monitor shows value = 4095
///
int analogPin = D0;  //  Pin where the analogRead is connected P26/A0/D0

void setup() {
    Serial.begin(115200);  // Start serial communication at 115200 baud rate
    analogReadResolution(12);  // Set ADC resolution to 12-bit (0-4095)
}

void loop() {
    int sensorValue = analogRead(analogPin);  // Reads the voltage from pin A0 (0V to 3.3V) and converts it into a 12-bit value (0-4095)

    Serial.println(sensorValue);  // Print the value to the Serial Monitor
    delay(500);  // Wait 500 milliseconds before the next reading
}
The D0 pin is a digital pin, but it can also be used as an analog input. However, it may have lower stability and accuracy compared to the A0 pin.

I2C Scanner

#include <Wire.h>

void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);  // wait for serial monitor
  Serial.println("\nI2C Scanner");
}

void loop() {
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for (address = 1; address < 127; address++) {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.print(address, HEX);
      Serial.println("  !");

      nDevices++;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);  // wait 5 seconds for next scan
}

When the port suddenly stops being recognized in Arduino IDE

  1. Download Blink.ino.uf2
  2. Press Boot Button of Xiao and connect to PC
  3. Drag uf2 file to RPI-RP2 drive
    alt text
  4. After the Blink code run, the port will be recognized
  1. Open an example code such as Blink
    or Copy below
    // Blink
    void setup() {
      pinMode(LED_BUILTIN, OUTPUT);
    }
    void loop() {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);
      digitalWrite(LED_BUILTIN, LOW);
      delay(1000);
    }
    
  2. Compile the code
  3. Find the path to Blink.ino.uf2 alt text
  4. Open terminal and open the folder
    % pwd
    /Users/yuichi
    % cd /Users/yuichi/Library/Caches/arduino/sketches/967F6001D67A7E915EC42BA164079EB8
    % open .
    

alt text
5. Press Boot Button of Xiao and connect to PC
6. Drag uf2 file to RPI-RP2 drive
alt text
7. After the Blink code run, the port will be recognized