Beetle RP2040 Board Wiki - DFRobot

Introduction

Designed by Raspberry Pi, RP2040 features a dual-core ARM Cortex-M0+ processor with up to 133MHz clock and 264KB internal RAM, and supports USB 1.1 devices, which allows users to focus on function implementation instead of spending too much time and effort on improving codes.
DFRobot Beetle RP2040 is a development board built around the RP2040 chip. It has a compact size of 27mm×20mm, which is easy to be embedded into small devices or projects. The board adopts a beginner-friendly large pad design, effectively reducing the difficulty of soldering. And it provides 8 carefully-selected GPIO ports that can serve as I2C, UART, SPI, digital port, analog port, etc., more convenient and flexible for users to develop various applications.

Features

Application

Function Indication

Name Description Remarks
USB Download Port and Power Supply Port Download program and power the main controller Interface type: TYPE-C
RP2040 Raspberry Pi RP2040 main controller chip
Reset Button When pressed, the program performs the reset operation
BOOT Button Keep pressing the BOOT button and connect USB cable to the computer, then the computer will eject the USB flash drive This button is used when the program cannot be used or downloaded properly
LED Onboard LED for test, connected to pin 13 Green

BOOT button: Sometimes RP2040 program running may work wrongly, and this will cause program error and download failure. The solution to that is holding the BOOT button and plugging in the USB cable to the computer at the same time, then the USB drive will be ejected. The relevant port selection in Arduino IDE turns grey and cannot be chosen, but the Arduino program can be download normally without selecting port.

RP2040 can select port normally after one successful program downloading.

Shown as below:

Select Port

Pinout

NO. Digital Port Analog Port UART I2C SPI Others
GP0 D0 SPI0/MISO
GP1 D1 SPI0/CSn
GP2 D2 SDA1 SPI0/SCK When using SPI0, I2C1 is not available
GP3 D3 SCL1 SPI0/MOSI
GP4 D4 RX2 SDA0 When using I2C0, UART2 is not available
GP5 D5 TX2 SCL0
GP28 D28 A2 TX1
GP29 D29 A3 RX1
GND Ground pin
VCC This pin is connected to the USB power pin, when used as an output, it equals USB voltage (generally 5V); when used as an input, the supply voltage is 3.3V~5V.
3V3 This pin is connected to the output of the buck chip. It can be used as a power supply output with a voltage of 3.3V.

Dimension Diagram

Specification

Arduino Tutorial

Requirements

Download Arduino IDE

Click into the Arduino website to download the Arduino IDE. Choose the suitable IDE package according to your computer system.

Download and Install SDK

Click the menu "File", then click "Preferences" to enter the Preferences window.

Click the icon marked by the arrow in the Preferences window.

In the pop-up window, add a json link in a new line:
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

Download SDK

Click the drop-down menu as shown in the figure below to enter the "Boards Manager" window.

Enter "RP2040" in the search box to find and install the SDK named "Raspberry Pi Pico/RP2040", as marked by the arrow in the picture below.

Select the Development Board

Click the Tools menu and select the "DFRobot Beetle RP2040" development board as shown in the figure below.

The installation of Beetle RP2040 SDK is complete.

Light LED

Function Description

There is an onboard LED on the upper left corner of the Beetle RP2040, and a silkscreen "L" marked beside it. The LED is used to quickly verify the main control board and the code, as shown in the figure below:

Configured the same as the LED of ArduinoUNO, the LED of Beetle RP2040 is also connected to pin 13. So you can directly use the Blink routine in the ArduinoIDE.
The following routine will describe how to light up the onboard LED.

Sample Code

Function: make the onboard LED blink at 1 second intervals.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);//set the pin to output mode
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH); //write high level to the port
  delay(1000);                    // delay for one second
  digitalWrite(LED_BUILTIN, LOW);  //write low level to the port  
  delay(1000);                   //delay for one second    
}

Result

The onboard LED blinks repeatedly at 1 second intervals.

Digital Pins

The Beetle RP2040 provides 8 GPIOs, each of which can be used as digital input and output. The functions are listed in the following table:

NO. Arduino Digital Port Output Input PWM Internal Pull-up Resistor
GP0 D0
GP1 D1
GP2 D2
GP3 D3
GP4 D4
GP5 D5
GP28 D28
GP29 D29

Write digital pins

Hardware Connection

Sample Code

Function: make the LED connected to GP0 blink at one second intervals.

int digital_test =0; 

void setup() {
  pinMode(digital_test, OUTPUT); //set the pin to output mode
}

void loop() {
  digitalWrite(digital_test, HIGH); //high level for lighting up the LED
  delay(1000);                    
  digitalWrite(digital_test, LOW); //low level for turning off the LED  
  delay(1000);                       
}
Result

After downloading the code, the LED connected to GP0 will blink at one-second intervals. If the LED is not lit, please check the hardware connection according to the connection diagram.

Read Digital Pins

When the digital pins are used as inputs, the port levels need to be set as pull-up or pull-down to ensure a stable high/low port level. The RP2040 can use both external pull-up and internal pull-up.

External Pull-up Resistor

Hardware Connection

Sample Code

Function: when the key connected to the P0 port is pressed, the onboard LED lights up; when released, the LED goes out.

const int buttonPin = 0;     
const int ledPin = 13;      

int buttonState = 0;         

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);//set the button pin to input.
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, LOW);
  } else {
    digitalWrite(ledPin, HIGH);
  }
}
Result

After downloading the code, the onboard LED lights up when the KEY connected to GP0 is pressed, and goes out when released. If the LED is not lit, please check the hardware connection according to the connection diagram or check the resistance correction.

Internal Pull-up Resistor

In the last routine, R1 was used as an external pull-up resistor to ensure the stability of the port. There is a function to set the internal pull-up resistor inside the RP2040 chip. In this case, the external pull-up resistor of R1 can be eliminated to simplify the construction of the hardware circuit.

Hardware Connection

R: current-limiting resistor for the key, connect between the key and GP0 with a resistance of 220Ω.
KEY: control key, connect between the current-limiting resistor R2 and GND. When pressed, it pulls GP0 down to a low level.

Sample Code

Function: use the statement pinMode(buttonPin, INPUT_PULLUP) to set port GP0 to internal pull-up. The onboard LED lights up when the key connected to port GP0 is pressed and goes out when released.

const int buttonPin = 0;     
const int ledPin = 13;      

int buttonState = 0;         

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); //set the key port as input and internal pull-up.
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(ledPin, LOW);
  } else {
    digitalWrite(ledPin, HIGH);
  }
}
Result

After downloading the code, the onboard LED lights up when the KEY connected to GP0 is pressed, and goes out when released.
Change pinMode(buttonPin, INPUT_PULLUP) to pinMode(buttonPin, INPUT) to cancel the internal pull-up and re-download the program, after that, the onboard LED is on and can’t work normally, which means the port supports internal pull-up. After testing one by one, it proved that the 8 ports of Beetle RP2040 all support internal pull-up.

Read Analog Pins

Beetle RP2040 has 2-way ADC pins, GP28(A2) and GP29(A3). It can read external analog quantity with ADC of 0-3.3V and ADC precision 10 bits, corresponding to the range of 0-1023 .

Hardware connection

R: Potentiometer. The value is over 100K. The adjustable end is connected to GP28(A2), and fixed end to 3V3 and GND.

Sample Code

Function: The code will read the potentiometer connected to GP28(A2) and print the value of the rotary potentiometer in real time in the serial monitor.

const int analogInPin = A2;  //hardware connected to GP28(A2)
int sensorValue = 0;        

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(analogInPin);
  Serial.println("sensor = ");
  Serial.println(sensorValue);
  delay(200);
}
Result

Open the serial monitor, and the window prints the current potentiometer value. The printed value changes as the potentiometer is rotated to change the angle.

PWM Output (Analog Write)

PWM (Pulse Width Modulation) is an effective technique to control analog circuits through pulse width modulation using the digital interface of MCU. It is widely used in many fields such as lighting control, motor speed control, measurement, communication, and power control and conversion.

The Beetle RP2040 supports PWM output on the 8 GPIOs. The PWM output is defined as an analog output in the Arduino development environment and is controlled using analogWrite() function. The range of PWM values is 0~255.

The following sample will demonstrate how to use PWM to drive the onboard LED to present a breathing lighting effect. No external hardware is required for this sample.

Sample Code

Function: use PWM to drive the onboard LED to present a breathing lighting effect

void setup(){
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop(){

byte value;

  for (value=0; value<255; value++)
  { 
    analogWrite(LED_BUILTIN, value);
    delay(10);
  } 

  for (value=255; value>0; value--)
  { 
    analogWrite(LED_BUILTIN, value);
    delay(10);
  }  
}
Result

After downloading the code, the onboard green LED gradually gets bright and then fades.

Servo Control

PWM can be used not only to drive the LED light and motor, but also to drive the servo. The official Arduino provides the library for servo control, which can be used to control the rotation angle of the servo.
The following sample shows how to drive the servo by Beetle RP2040.

Hardware Connection

Sample Code

Function: make the servo connected to the GP3 port turn from 0° to 180°, and then back to 0° from 180°. Repeat the cycle.

#include <Servo.h>

Servo myservo; 

void setup() {

  myservo.attach(3);  // set the port number connected to the servo as D3

}

void loop() {

  int pos;

  for (pos = 0; pos <= 180; pos += 1) { 
    myservo.write(pos);             
    delay(15);                       
  }

  for (pos = 180; pos >= 0; pos -= 1) {
    myservo.write(pos);              
    delay(15);                       
  }

}
Result

The servo slowly turns from 0° to 180°, and then returns to 0° from 180°, repeating the cycle.

UART Communication

In Arduino development environment, Beetle RP2040 defines 3 groups of UART communication interfaces: UART0, UART1, UART2. USB-to-serial port is defined as UART0, which does not conflict with UART1 and UART2.

NO. UART Functions Other Functions
GP4 Serial2 / RX SDA0
GP5 Serial2 / TX SCL0
GP28 Serial1 / TX A2
GP29 Serial1 / RX A3
USB Serial

The following is the definition of the UART interface in "pins_arduino.h" file.

#define PIN_SERIAL1_TX (28u)
#define PIN_SERIAL1_RX (29u)

#define PIN_SERIAL2_TX (4u)
#define PIN_SERIAL2_RX (5u)

The following sample will demonstrate how to read the value of the sensor with UART interface using UART1 and print it in the serial monitor of ArduinoIDE. The environmental sensor (SKU:SEN0500) is used in this sample. The sensor supports both UART and I2C communications and can detect the five environmental parameters: UV, light intensity, temperature, humidity, and atmospheric pressure.

Multifunctional Environmental Sensor (Breakout):

DFRobot Forum- Sensors- Temperature & Humidity Sensor- Fermion: Multifunctional Environmental Sensor (Breakout)

Multifunctional Environmental Sensor (Breakout) Wiki:

DFROBOT SEN0500 Fermion: Multifunctional Environmental Sensor (Breakout) Documents Tutorials

Hardware Connection

The UART communication of the environmental sensor is used in this example. Connect the UART port as follows:

BeetleRP2040: GP29(RX) (connect to) Environmental Sensor: TX(T)

BeetleRP2040: GP28(TX) (connect to) Environmental Sensor: RX(R)

BeetleRP2040: 3V3 (connect to) Environmental Sensor: 3V3

BeetleRP2040: GND (connect to) Environmental Sensor: GND

Software Preparation
Sample Code

Function: read the value of the environmental sensor and print the readings in the serial port.

#include "DFRobot_EnvironmentalSensor.h"

DFRobot_EnvironmentalSensor environment(/*addr =*/SEN050X_DEFAULT_DEVICE_ADDRESS, /*s =*/&Serial1); //define the UART interface connected to the sensor

void setup()
{
  environment.begin();  //initialize the sensor
  Serial1.begin(9600); //define the baud rate of the UART interface connected to the sensor
  Serial.begin(9600); //define the baud rate of the USB-to-serial interface
}

void loop()
{
  Serial.println("-------------------------------");
  Serial.print("Temp: ");
  Serial.print(environment.getTemperature(TEMP_C));
  Serial.println(" ℃");
  Serial.print("Temp: ");
  Serial.print(environment.getTemperature(TEMP_F));
  Serial.println(" ℉");
  Serial.print("Humidity: ");
  Serial.print(environment.getHumidity());
  Serial.println(" %");
  Serial.print("Ultraviolet intensity: ");
  Serial.print(environment.getUltravioletIntensity());
  Serial.println(" mw/cm2");
  Serial.print("LuminousIntensity: ");
  Serial.print(environment.getLuminousIntensity());
  Serial.println(" lx");
  Serial.print("Atmospheric pressure: ");
  Serial.print(environment.getAtmospherePressure(HPA));
  Serial.println(" hpa");
  Serial.print("Altitude: ");
  Serial.print(environment.getElevation());
  Serial.println(" m");
  Serial.println("-------------------------------");
  delay(500);
}
Result

Open the ArduinoIDE serial monitor, and the monitor window prints the sensor data correctly.

I2C Communication

Developed by Philips, the I2C bus is a simple, bidirectional two-wire synchronous serial bus. It requires only two wires to transfer information between the devices connected to the bus.

The Beetle RP2040 provides two groups of I2C communication interfaces: I2C0 and I2C1. The pinouts are as follows:

NO. I2C Function Other Functions
GP2 I2C1/SDA SPI0/SCK
GP3 I2C1/SCL SPI0/MOSI
GP4 I2C0/SDA0 RX2
GP5 I2C0/SCL0 TX2

The default I2C interface is I2C0 in Arduino because I2C1 overlaps with some pins of SPI while I2C0 is only multiplexed with the seldom-used UART1.

The following sample will demonstrate how to use the I2C interface to read the temperature and humidity values from the AHT20 temperature and humidity sensor. The sensor supports 3.3V~5V power supply and I2C communication, and provides Arduino library for direct use. Other I2C devices are used in a similar way.

AHT20 Temperature and Humidity Sensor:

AHT20 Temperature and Humidity Sensor Wiki: SEN0527_Product Information Tutorial

Hardware Connection

AHT20: pin SDA (connect to) BeetleRP2040: pin GP4

AHT20: pin SCL (connect to) BeetleRP2040: pin GP5

AHT20: pin GND (connect to) BeetleRP2040: pin GND

AHT20: pin VCC (connect to) BeetleRP2040: pin 3V3

Sample Code

Before writing the code, first download and install the AHT20 library. Library download link: https://github.com/cdjq/DFRobot_AHT20

Function: read the temperature and humidity values from the AHT20 temperature and humidity sensor and print them in the serial port.

#include "DFRobot_AHT20.h"

DFRobot_AHT20 aht20;

void setup(){
  Serial.begin(9600);
  while(!Serial){
  }
  uint8_t status;
  while((status = aht20.begin()) != 0){
    Serial.print("AHT20 sensor initialization failed. error status : ");
    Serial.println(status);
    delay(1000);
  }
}

void loop(){
  if(aht20.startMeasurementReady(true)){
    Serial.print("temperature(-40~85 C): ");
    Serial.print(aht20.getTemperature_C());
    Serial.print(" C, ");
    Serial.print(aht20.getTemperature_F());
    Serial.print(" F\t");
    Serial.print("humidity(0~100): ");
    Serial.print(aht20.getHumidity_RH());
    Serial.println(" %RH");
    delay(8000);
  }
}
Result

Open the serial monitor of Arduino IDE, set the baud rate to 9600, and the window prints the temperature and humidity information read by the sensor.

SPI Communication

The SPI (Serial Peripheral Interface) is a synchronous peripheral interface that allows the MCU to serially communicate with various peripheral devices to exchange information. Peripheral devices include Flash, RAM, LCD display, A/D converter, MCU, etc.

Beetle RP2040 supports full-duplex SPI communication. The pinouts are as follows:

No. SPI Function Other Functions
GP0 SPI0/MISO
GP1 SPI0/CSn
GP2 SPI0/SCK SDA1
GP3 SPI0/MOSI SCL1

The following sample will demonstrate how to drive the LCD display to show the pattern using SPI. In this sample, a 1.8" 128x160 TFT LCD display is used.

The Display Link:

DFRobot Forum-Modules- LCDs/LEDs/E-ink/Displays- Screens & Displays- Fermion: 1.8" 128x160 IPS TFT LCD Display with MicroSD Card Slot (Breakout)

The Display Wiki:

Fermion: 1.8" 128x160 IPS TFT LCD Display with MicroSD Card Slot (Breakout)

Hardware Connection

BeetleRP2040: GP0 (connect to) Display: MISO

BeetleRP2040: GP2 (connect to) Display: SCLK

BeetleRP2040: GP3 (connect to) Display: MOSI

BeetleRP2040: GP4 (connect to) Display: DC

BeetleRP2040: GP5 (connect to) Display: CS

BeetleRP2040: GP28 (connect to) Display: RES

BeetleRP2040: GND (connect to) Display: GND

BeetleRP2040: 3V3 (connect to) Display: VCC

Sample Code

Function: make the LCD display connected to the SPI of the RP2040 display the text "DFRobot".

#include "DFRobot_GDL.h"

#define TFT_DC  4
#define TFT_CS  5
#define TFT_RST 28

DFRobot_ST7735_128x160_HW_SPI screen(/*dc=*/TFT_DC,/*cs=*/TFT_CS,/*rst=*/TFT_RST);

void setup() {
  screen.begin();
  screen.setTextSize(1);
  screen.fillScreen(COLOR_RGB565_BLACK);
  screen.setFont(&FreeMono12pt7b);
  screen.setCursor(/*x=*/32,/*y=*/64);
  screen.setTextColor(COLOR_RGB565_LGRAY);
  screen.setTextWrap(true);
  screen.print("DFRobot");
}

void loop() {

}
Result

The display shows the text "DFRobot" in the center of the screen.

FAQ

For any questions, advice or cool ideas to share, please visit the DFRobot Forum.

More Documents

DFR0959-RP2040 Datasheet-EN.pdf

DFR0959-Beetle-RP2040(V1.0)-SCH.PDF

DFR0959-Beetle-RP2040(V1.0)-SCH.PDF

PICO-Python-SDK.pdf