5. Arduino projects
5.1 Blink an LED
Use an Arduino digital output to blink the onboard LED. This first exercise uses the LED already on the ColorEasyDuino board.
If you have not set up the Arduino IDE yet, start with Arduino IDE setup.
5.1.1 LED parts
An LED (light-emitting diode) is a semiconductor light source. Its main parts are:
- Housing: Usually made of plastic or glass, used to protect internal components.
- Emitting Material: The core part of the LED, made from special semiconductor materials such as the common InGaN (Indium Gallium Nitride) or AlInGaP (Aluminum Indium Gallium Phosphide).
- Chip: The light-emitting diode chip that generates light.
- Leads: Metal wires that provide electrical connections.
- Solder Points: The soldered points that connect the LED chip to the leads.
- Electrodes: Responsible for connecting the semiconductor material to the external circuit, typically made of metal.
- Reflector Cavity: A structure used to enhance the light emission effect by reflecting the emitted light to the front.
5.1.2 How LEDs emit light
An LED has a junction between n-type and p-type semiconductor materials. When forward-biased, electrons and holes recombine at the junction and release light as photons.
5.1.3 Drive an LED
LEDs need a controlled current. A constant-current driver keeps it steady, and a series resistor limits current when driving an LED from a voltage source. Too much current can damage the LED.
A GPIO can source current to the LED or sink current from it. The diagrams below show both connections.
- Sourcing current: The GPIO pin supplies current to the LED when the pin is HIGH.
- Sinking current: Current flows from the supply through the LED into the GPIO pin when the pin is LOW.
Forward voltage varies by LED color. Use a series resistor to limit the current. For a simple indicator circuit, a typical resistor is between 220 Ω and 10 kΩ. A higher resistance makes the LED dimmer.
5.1.4 Board schematic
On this board, the LM358 supplies current to the onboard LED.
The LM358 is a dual operational amplifier. Here it acts as a comparator: pin 7 goes high when pin 5 is above pin 6, and low when pin 5 is below pin 6.
The schematic labels GPIO13 as SCK. Drive GPIO13 HIGH to turn on the onboard LED and LOW to turn it off.
5.1.5 Digital output
Use pinMode() to set GPIO13 as an output, then call digitalWrite() to turn the LED on or off.
5.1.6 Set the pin mode
Use pinMode() to configure a digital pin as an input or output.
pinMode(pin,mode);Pass the pin number and one of these modes:
INPUT: Read an external signal. Use a pull-up or pull-down resistor to keep an unconnected input from floating.INPUT_PULLUP: Enable the internal pull-up resistor. The pin reads HIGH until an external circuit pulls it LOW.OUTPUT: Drive the pin HIGH or LOW.INPUT_PULLDOWN: On boards that support it, enable the internal pull-down resistor so an unconnected input reads LOW.
For example:
// Set pin 6 as output mode
pinMode(6, OUTPUT);
// Set pin 2 as input mode
pinMode(2, INPUT);2
3
4
5.1.7 Set the pin level
Use digitalWrite() to set an output pin HIGH or LOW. The syntax of the function is:
digitalWrite(pin, value);pinis the digital pin number.valueisHIGHorLOW.
For example:
pinMode(13, OUTPUT); // Set pin 13 to OUTPUT mode
digitalWrite(13, HIGH); // Set digital pin 13 output to HIGH (high level)2
Set the pin to
OUTPUTbefore usingdigitalWrite()to drive an LED.
❓What are digital pins?
Digital pins read or drive two logic levels: HIGH and LOW. The voltage for HIGH depends on the board.
5.1.8 Upload the sketch
- Open the Arduino IDE.
- Connect the board to your computer with a USB data cable.
- Paste the sketch into the IDE.
// Define the Arduino pin connected to the positive pole of the LED
const int ledPin = 13;
// The setup function runs when the Arduino board is restarted or powered on
void setup()
{
// Set the mode of ledPin pin to output mode
pinMode(ledPin, OUTPUT);
}
// The loop function continues to run in a loop after the setup is run.
void loop()
{
// Turn on the LED
digitalWrite(ledPin, HIGH);
// Wait for 1000 milliseconds (1 second)
delay(1000);
// turn off LED
digitalWrite(ledPin, LOW);
// Wait for 1000 milliseconds (1 second)
delay(1000);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- Select your board and port. If you are unsure which port to use, unplug the board and check which port disappears.
- Click Upload.
5.1.9 Check the LED
After uploading, check that the LED stays on for one second, then off for one second. The pattern repeats.
5.2 Read a push button
Use a push button as a digital input. The LED turns on while you hold the button and turns off when you release it.
Parts
- 1 x ColorEasyDuino development board
- 1 x breadboard
- 1 x LED light
- 1 x 220Ω resistor
- 1 x button
- 10 kΩ resistor (optional external pull-up shown in the wiring diagram)
- Jumper wires
5.2.1 Push buttons
A push button is a mechanical switch. This example uses a normally open button that closes only while you press it.
5.2.2 Inside a button
Pressing the button moves a spring or metal contact to close the circuit. Releasing the button lets the contact spring back and opens the circuit.
5.2.3 Button states
Connect the button to a GPIO input and use a pull-up or pull-down resistor to set the idle level. The input changes state when the button closes.
5.2.4 Debounce a button
Mechanical contacts can open and close several times during a single press or release. Hardware or software debouncing filters out those extra transitions.
- Software Debouncing: This approach uses programming techniques to set a delay or timer, ensuring that the key state is read only once within a specified time frame, thereby avoiding the effects of bouncing on the program.
- Hardware Debouncing: This involves adding components such as resistors and capacitors to the key circuit to form an RC low-pass filter, which smooths out the key signal and reduces the impact of bouncing.
5.2.5 Wiring
Step 1: Connect the LED
- Connect the LED’s long leg (anode) to D13.
- Connect the short leg (cathode) to GND through a 220 Ω resistor.
Step 2: Connect the button
Connect one switch contact to GND and the other to D2. Use the two contacts that connect only when you press the button.
The sketch enables the internal pull-up with
INPUT_PULLUP. You can leave out the optional external pull-up shown in the diagram.
Wiring diagram
In the board schematic, R1 pulls GPIO2 up to +5 V. The button connects GPIO2 to GND when pressed.
GPIO2 reads HIGH when the button is released and LOW when pressed. R1 limits current while the button is closed.
5.2.6 Read a digital pin
Set the pin as an input and call digitalRead(pin) to read HIGH or LOW. This sketch checks the pin each time through loop(). That is called polling.
Read pin 2:
int buttonState = digitalRead(2);The example reads GPIO2 and stores HIGH or LOW in buttonState.
5.2.7 Upload the sketch
- Open the Arduino IDE.
- Connect the board to your computer with a USB data cable.
- Paste the sketch into the IDE.
// Define the pins connected to the LED and button
const int buttonPin = 2; // Button connected to digital pin 2
const int ledPin = 13; // LED connected to digital pin 13
// Variable to track button state
int buttonState = 0;
void setup() {
// Initialize LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize button pin as an input with pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Enable internal pull-up resistor if not using a physical one
}
void loop() {
// Read the button state
buttonState = digitalRead(buttonPin);
// If the button is pressed
if (buttonState == LOW) {
// Turn on the LED
digitalWrite(ledPin, HIGH);
} else {
// Otherwise, turn off the LED
digitalWrite(ledPin, LOW);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- Select your board and port.
- Click Upload.
- Hold the button down, then release it.
5.2.8 Test the button
Press and hold the button. The LED should light up. Release the button to turn it off.
5.3 Time Arduino tasks
Blink an LED with delay(), then use millis() or micros() to time work while the main loop keeps running.
5.3.1 When to use delays
Delays give hardware time to respond or space out actions such as LED blinks and sensor readings. delay() pauses the main sketch. If the sketch also needs to check buttons or read serial input, track elapsed time with millis() or micros() instead.
5.3.2 Wait with delay()
delay() pauses the main sketch for the given number of milliseconds.
delay(milliseconds);The sketch below blinks the LED:
// Define the Arduino pin connected to the LED's positive terminal
const int ledPin = 13;
// The setup function runs when the Arduino board is reset or powered on
void setup()
{
// Set the mode of the ledPin to OUTPUT
pinMode(ledPin, OUTPUT);
}
// The loop function runs continuously after setup
void loop()
{
// Turn on the LED
digitalWrite(ledPin, HIGH);
// Wait for 1000 milliseconds (1 second)
delay(1000);
// Turn off the LED
digitalWrite(ledPin, LOW);
// Wait for 1000 milliseconds (1 second)
delay(1000);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
The LED stays on for one second and off for one second.
5.3.3 Non-blocking timing
millis() returns the number of milliseconds since startup or reset as an unsigned long.
Check the elapsed time on each pass through loop() so other code can run between checks.
unsigned long previousMillis = 0; // Store the last update time
const long interval = 1000; // Set the interval time (1 second)
void setup() {
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
if (currentMillis - previousMillis >= interval) { // Check if 1 second has passed
previousMillis = currentMillis; // Save the current time
// Perform timed actions here
}
// Other code can be added here to execute between timed actions
}2
3
4
5
6
7
8
9
10
11
12
13
14
When at least one second has elapsed, the sketch updates previousMillis. Add the action you want to run inside the if block. As written, the sketch updates the timer but does not blink the LED or print a message.
5.3.4 Time with micros()
micros() returns the number of microseconds since the sketch started. The example stores a timestamp. To measure an operation, take another reading after it finishes and subtract the first. This sketch does not print the result.
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long timeMicros = micros();
// Execute operations...
// You can then use timeMicros to determine the time spent in microseconds
}2
3
4
5
6
7
8
5.3.5 Timer rollover
- On this board,
millis()rolls over after about 50 days andmicros()after about 70 minutes. - Compare elapsed time by subtracting the saved timestamp from the current reading, as in the
millis()example.
5.4 Dim an LED with PWM
Read the voltage from a potentiometer and use PWM to adjust an LED’s brightness.
Materials List
- 1 x Development board
- 1 x Breadboard
- 1 x LED
- 1 x 220Ω Resistor
- 1 x 10kΩ Potentiometer
- Jumper wires (several)
5.4.1 Read analog input
The board’s 10-bit analog-to-digital converter (ADC) maps 0–5 V on pins A0–A5 to a reading from 0 to 1023. Here are some key points about analog input:
- Resolution: The board’s 10-bit ADC provides 2^10 = 1024 different voltage levels. This means it can distinguish changes of approximately 4.88 mV (Vref/1024), where Vref is typically 5V, depending on the supply voltage of the board.
- Voltage Reference: By default, the reference voltage for analog inputs is 5V (on 5V powered Arduino boards), but this value can be changed programmatically or using an external reference.
- Reading Values: You can read the voltage value of an analog pin using the analogRead(pin) function, where pin is the number of the analog pin you wish to read (e.g., A0, A1, etc.).
- Applications: Analog input pins can be used to read data from various types of sensors, such as temperature, humidity, light intensity, sound intensity, and position (using a potentiometer).
5.4.2 PWM output
Use pulse-width modulation (PWM) to vary LED brightness.
Pulse Width Modulation (PWM)
PWM switches a digital output on and off rapidly. The duty cycle is the percentage of each cycle that the output stays HIGH. A 50% duty cycle keeps the output HIGH for half of each cycle. A higher duty cycle makes the LED brighter.
- Frequency is the number of cycles per second. Many Arduino PWM pins run at around 490 Hz.
- Output is set with
analogWrite(pin, value). The value ranges from 0 (always LOW) to 255 (always HIGH). - Uses include dimming LEDs, controlling motor speed, and adjusting heating power.
Reminders:
- While PWM can simulate analog output, it does not produce a true continuous analog signal. For specific applications (like generating precise audio signals), true analog output may be required, necessitating the use of an external Digital-to-Analog Converter (DAC).
- Since PWM signals are inherently still digital signals (high-frequency “on” and “off”), certain applications may require a low-pass filter to smooth the output signal, making it closer to a true analog signal, especially when fine control without PWM noise is desired.
- Only certain Arduino pins can generate PWM signals. Typically, these pins are marked with a “~” symbol on the Arduino board.
5.4.3 Wiring
Step 1: Connect the LED
- Insert the longer leg (anode) of the LED into a slot on the breadboard.
- Connect a 220Ω resistor from the shorter leg (cathode) of the LED to the GND pin on the Arduino.
- Connect the LED’s long leg (anode) to D9, the PWM output used by the sketch.
Note: Be mindful of pin selection; PWM output pins are typically marked with a “~” symbol.
Step 2: Connect the Potentiometer
- Connect one terminal of the potentiometer to +5V.
- Connect the other terminal to GND.
- Connect the potentiometer’s middle pin (wiper) to A0, the analog input used by the sketch.
Analog input pins are available from A0 to A5.
5.4.4 Upload the sketch
Paste this sketch into the Arduino IDE.
// Define the analog input pin connected to the potentiometer
const int potPin = A0;
// Define the digital pin connected to the LED, which needs to support PWM
const int ledPin = 9;
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the value from the potentiometer (between 0 and 1023)
int potValue = analogRead(potPin);
// Map the read analog value to the PWM range (between 0 and 255)
int ledBrightness = map(potValue, 0, 1023, 0, 255);
// et the brightness of the LED
analogWrite(ledPin, ledBrightness);
// Delay for 100 milliseconds to see the brightness change
delay(100);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
In the code above, there's a line with a function that might be unfamiliar:
c// Map the read analog value to the PWM range (between 0 and 255) int ledBrightness = map(potValue, 0, 1023, 0, 255);1
2In Arduino, the
map()function is used to map a number from one range to another. Here’s what each parameter means:a.
potValueis the current value read from the analog input, which may come from something like a potentiometer (rotary position sensor) or other analog sensors.b.
0is the minimum possible value ofpotValue, corresponding to the minimum voltage from the analog input (usually 0V).c.
1023is the maximum possible value ofpotValue, corresponding to the maximum voltage from the analog input (in a 10-bit ADC, this is usually the maximum digital representation for 5V or 3.3V). d.0is the minimum value of the mapped range, which is the value you want to mappotValue's minimum to. e.255is the maximum value of the mapped range, which is the value you want to mappotValue's maximum to. This line of code thus converts the value read from the analog input pin (assumed to be between 0 and 1023) to a value between 0 and 255. This is commonly used to convert a 10-bit analog input value into an 8-bit value suitable for PWM output (usinganalogWrite), asanalogWriteaccepts values from 0 to 255 to represent the duty cycle of the PWM. The mapping process is linear, meaning ifpotValueis 512, the mapped value will be about 128, since 512 is roughly half of 1023, and similarly, 128 is half of 255.
Connect the board over USB, select your board and port, then click Upload.
5.4.5 Test the dimmer
Turn the potentiometer after uploading the sketch. The LED should change brightness as the sketch maps the analog reading to a PWM value.
5.5 Handle interrupts
Use a button press to trigger an interrupt and toggle an LED.
5.5.1 How interrupts work
An interrupt pauses the current code and runs an interrupt service routine (ISR). The sketch resumes when the ISR returns. Hardware events such as timer expiry or a pin change can trigger interrupts. Software instructions can trigger them too.
5.5.2 External interrupts
The button example in section 5.2 checks the input repeatedly. An external interrupt calls an ISR when the pin detects the configured signal change, so the main loop can do other work in the meantime.
5.5.3 Why use interrupts?
An interrupt helps capture a short or infrequent signal that the main loop might miss while it is busy. Keep the ISR short so other events can be handled promptly.
5.5.4 Wiring
Connect the LED and button
- Connect the LED’s long leg to D13 and its short leg to GND through a 220 Ω resistor.
- Connect the button between D2 and GND. The sketch uses D2 and enables its internal pull-up.
5.5.5 Attach an interrupt
On the Arduino Uno, pins 2 and 3 support external interrupts. Use attachInterrupt() to associate a pin event with an ISR.
Syntax
attachInterrupt(interruptNum, ISR, mode);interruptNumis the interrupt number. On the Uno, pin 2 maps to interrupt 0 and pin 3 to interrupt 1. UsedigitalPinToInterrupt(pin)to get the number from the pin.ISR: the function called when the interrupt is triggered.mode: specifies what type of pin state change triggers an interrupt. Commonly used modes are:LOW: triggered when the pin is low (not recommended as it will continuously trigger interrupts).CHANGE: triggered when the pin level changes.RISING: triggered when the pin level rises (from low to high).FALLING: triggered when the pin level falls (from high to low).
A rising edge is a change from LOW to HIGH. A falling edge is a change from HIGH to LOW. In this circuit, pressing the button pulls D2 LOW and triggers the falling-edge interrupt.

5.5.6 Upload and test
- Open the Arduino IDE.
- Connect the board to your computer with a USB data cable.
- Paste the sketch into the IDE.
const int buttonPin = 2; // External interrupt pin connected to digital pin 2
const int LEDPin = 13; // Debug LED connected to digital pin 13
volatile bool buttonPressed = false; // Shared variable marked with volatile keyword
bool led_flag = false;//LED state
void setup()
{
pinMode(buttonPin, INPUT_PULLUP); // Set input with internal pull-up resistor
pinMode(LEDPin , OUTPUT); // Set output mode
//Enable interrupt mode on pin 2, executing function handleInterrupt on falling edge
attachInterrupt(digitalPinToInterrupt(buttonPin), handleInterrupt, FALLING);
}
void loop()
{
if( buttonPressed == true )
{
digitalWrite(LEDPin, led_flag=!led_flag);
buttonPressed = false; // Reset status flag
}
// Execute other tasks
}
void handleInterrupt()
{
buttonPressed = true; // Update shared variable to indicate button has been pressed
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- Select your board and port.
- Click Upload.
- Press and release the button, then press it again. Each press should switch the LED to its other state.
Keep the ISR short
Set a flag in the ISR and do the work in loop(). Declare the flag volatile so the main code reads its current value. Keep delay() and serial printing in the main loop. Both can hold up the interrupt handler.
What happens on a press
The falling edge on D2 calls handleInterrupt(), which sets a flag. loop() reads the flag and switches the LED on or off.
5.6 Send and read serial data
Send a message from the board and echo incoming text in the Arduino IDE’s Serial Monitor.
5.6.1 TX and RX pins
TX transmits data and RX receives it. On the Arduino Uno, the hardware serial port uses digital pin 1 (TX) and pin 0 (RX). You can also access it over USB.
5.6.2 UART settings
Both ends of a UART connection need matching settings. A higher-level protocol such as Modbus defines how devices interpret the exchanged data.
UART sends one bit at a time. A frame starts with a LOW start bit, followed by data bits, optional parity, and a HIGH stop bit. The diagram shows a frame with eight data bits and one stop bit.
- Baud rate sets the signaling speed.
- Data bits set how many bits carry the data in each frame.
- Stop bits mark the end of the frame.
- Parity provides a simple error check.
5.6.3 Serial modes
A serial link can use one of three modes:
- Simplex: Data travels in one direction only.
- Half-duplex: Both ends can send, but only one at a time.
- Full-duplex: Both ends can send and receive at the same time.
5.6.4 Why use serial?
Use serial to read sensor data, send commands, or print debug messages. Serial links also support remote monitoring and control.
5.6.5 Connect over USB
Connect the board to your computer with a USB data cable. The onboard CH340 handles the USB-to-serial connection.
If the computer does not detect a serial port, check whether your operating system needs a CH340 driver.
INFO
Compressed Package
5.6.6 Serial functions
Initialize Serial Port
Call Serial.begin(baudRate) with the same baud rate as the device at the other end.
void setup() {
Serial.begin(9600); // Set the baud rate to 9600
}2
3
Send Data
Use Serial.print() to send data. Use Serial.println() to add a line ending.
void loop() {
Serial.println("Hello, world!"); // Send string and add newline
delay(1000); // Send once per second
}2
3
4
Receive Data
Check Serial.available() for incoming data, then read a byte with Serial.read().
void loop() {
if (Serial.available() > 0) {
char received = Serial.read(); // Read and store one byte of data
Serial.print("Received: ");
Serial.println(received); // Display the received data
}
}2
3
4
5
6
7
Advanced Reading
Serial.readString(): Reads the characters received via the serial port until a timeout occurs. This method depends on the timeout set bysetTimeout().Serial.readStringUntil(terminator): Reads serial data until a specified terminator character is encountered or a timeout occurs.Serial.parseInt(): Reads data from the serial port and parses it as aninttype. This is very convenient for receiving numeric data.Serial.parseFloat(): Reads data from the serial port and parses it as afloattype. This is suitable for situations where floating-point numbers need to be handled.
5.6.7 Upload the sketch
Paste this sketch into the Arduino IDE, select your board and port, then click Upload.
void setup() {
Serial.begin(9600); // Initialize the serial port
}
void loop() {
// Send data
Serial.println("Sending data...");
delay(1000); // Wait for one second
// Check if there is data available to read
if (Serial.available() > 0) {
String receivedData = Serial.readStringUntil('\n'); // Read data until a newline character
Serial.print("Received: ");
Serial.println(receivedData); // Print the received data
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
5.6.8 Test serial I/O
The sketch prints a message, waits one second, then checks for incoming text and echoes it.
Open Serial Monitor from the top right of the Arduino IDE.
- Set the baud rate to 9600 baud and the line ending to New Line. The sketch reads input up to the newline character.
- Check that
Sending data...appears in the output. - Type
abcdand press Enter. Look forReceived: abcdin the output.
5.7 Work with modules
5.7.1 OLED display
Parts and downloads
Purchase Link: https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-23284685151.19.1ffe61919S9gQy&id=40809409804
Baidu Netdisk Download Link: https://pan.baidu.com/s/1xy2zH8-hs-S8-_AcVtBP_g Password:0jhj
Specs
- Supply voltage: 3.3 V
- Operating current: 9 mA
- Module size: 27.3 × 27.8 mm
- Resolution: 128 × 64 pixels
- Driver: SSD1306
- Interface: I²C
- Header: 4 pins, 2.54 mm pitch
Wiring
- VCC → 3.3 V for the module listed above. Check the supply rating if you use a different module.
- GND → GND
- SCL → A5 (SCL)
- SDA → A4 (SDA)
Install libraries and upload the sketch
Install the libraries
- Open the Arduino IDE.
- Select “Tools” > “Manage Libraries...”.
- Install Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
Paste the sketch into the Arduino IDE, select your board and port, then click Upload.
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: March 28, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define screen width and height
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Create an Adafruit_SSD1306 object. The parameter is the reset pin; since many modules don’t use this, pass -1.
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the I2C address for most OLED screens; if it doesn’t work, try 0x3D
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Infinite loop
}
display.clearDisplay();
display.setTextSize(1); // Set text size
display.setTextColor(SSD1306_WHITE); // Set text color
display.setCursor(0,0); // Set text start position
display.println(F("Hello, world!"));
display.setTextSize(2);
display.setCursor(0,10);
display.println(F("SSD1306"));
display.display(); // Render all drawing operations
}
void loop() {
// No need for repeating code here
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Test the display
The screen should show Hello, world! and SSD1306. If it stays blank, check power, ground, and the two I²C connections. The sketch uses address 0x3C. Check your module’s address if the wiring matches the diagram.
5.7.2 DHT11 sensor
The DHT11 sends calibrated temperature and relative humidity readings over a single data wire.
Parts and downloads
Purchase Link: DHT11 Temperature Module | Humidity Module | Temperature and Humidity Module | DHT11 Sensor
Baidu Netdisk download link: https://pan.baidu.com/s/1HQEL699-Yl5Jh3Hp87_FlQ Password:2sgq
Specs
Operating voltage: 3-5.5V Operating current: 1MA Measurement resolution: 8 bit Communication protocol: Single bus Number of pins: 3 Pin (2.54mm pitch row of pins)
Wiring
- VCC to 5 V
- GND to GND
- DATA to digital pin 2 for this sketch
Check whether your module has a pull-up resistor between DATA and VCC. If it does not, follow the sensor’s wiring guide for the external pull-up.
Install the library and upload the sketch
Install the library
In the Arduino IDE’s Library Manager, search for DHT sensor library by Adafruit and install it. If the IDE prompts you to install dependencies, accept them.
Upload the sketch
Paste the sketch into the Arduino IDE, select your board and port, then click Upload.
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 8, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include "DHT.h"
#define DHTPIN 2 // Defines the DHT11 data pin to be connected to Arduino pin 2
#define DHTTYPE DHT11 // Define the DHT type as DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHT11 Temperature and Humidity test:");
// Initialize the DHT11 sensor
dht.begin();
}
void loop() {
// wait a few seconds between readings (DHT11 readings are relatively slow)
delay(2000);
// Read the temperature and humidity values
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if reading failed and read the temperature and humidity values.
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read DHT11!");
return;
}
// Output the read temperature and humidity.
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C ");
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Check the readings
Open Serial Monitor and select 9600 baud. The sketch prints relative humidity (%) and temperature (°C) about every two seconds. If you see Failed to read DHT11!, recheck VCC, GND, DATA on D2, and the data-line pull-up.
5.7.3 HC-05 Bluetooth
The HC-05 is a Bluetooth 2.0 + EDR serial module based on the CSR BC417. It uses GFSK in the 2.4 GHz ISM band. The 27 × 13 × 2 mm module has a PCB antenna, −85 dBm receive sensitivity, and a status LED. Use AT commands to set its role, baud rate, or name.
Parts and downloads
Specs
Operating Voltage: 3.6-6V
Supply Current: 40mA
Transmission Power: 4dBm (maximum)
Reference Range: 10 meters
Control Method: Serial
Number of Pins: 6 Pins (2.54mm pitch header)
Configure the module
If your HC-05 already accepts incoming connections and uses 9600 baud in data mode, skip to the bridge wiring below. To check or change those settings, use the onboard CH340 or a separate USB-to-serial adapter.
Hold down the module’s button while powering it on. On the module shown here, a slow blink indicates AT command mode.
AT wiring with the onboard CH340
The ColorEasyDuino schematic labels D0 and D1 from the ATmega328P’s point of view. D0/RX carries the CH340’s transmit signal, and D1/TX feeds the CH340’s receive input. The HC-05 signals therefore connect to the matching labels on the board when using the CH340.
- Unplug USB. Jumper the board’s RESET pin to GND and leave the jumper in place for the AT session. This keeps the ATmega328P from driving D0 or D1.
- Check the module’s UART voltage requirements. The onboard CH340 runs from USB’s 5 V supply. A breakout that accepts 5 V on VCC may still require 3.3 V on RXD. Use level shifting where required.
- Connect HC-05 TXD to D1/TX, HC-05 RXD to D0/RX, and GND to GND. Power the module according to its VCC rating. The breakout listed above accepts 5 V on VCC.
- Hold the HC-05 button while plugging in USB to power the module and enter AT mode.
If you use a separate USB-to-serial adapter, use one with signal levels suitable for the module. Connect adapter TX to HC-05 RXD, adapter RX to HC-05 TXD, and share GND. Power the module while holding its button.
In the serial tool, select the interface’s port. Set 38400 baud, 8 data bits, 1 stop bit, and no parity. End each command with carriage return and newline (\r\n). In the tool shown below, select Send New Line.
Check the connection and set the role
- Send
ATand look forOK. - Send
AT+ROLE=0so the phone can initiate the connection. SendAT+ROLE?to check that the response includes+ROLE:0.
Set the data-mode baud rate
Send AT+UART? to check the data-mode settings. The sketch expects 9600 baud. If the settings differ, send AT+UART=9600,0,0, then check again for +UART:9600,0,0. The two zeros select one stop bit and no parity. To check the pairing PIN, send AT+PSWD?.
Disconnect USB and power off the module before removing the AT wiring. If you used the onboard CH340, remove the RESET-to-GND jumper before uploading the bridge sketch. After uploading, pair your phone with HC-05 using the module’s PIN (1234 in the example above). The next two images show pairing and connecting in a Bluetooth serial app.
On the module shown here, the LED blinks rapidly in data mode before pairing and more slowly when the phone is connected.
Wire the bridge
With power disconnected, wire the module shown in this tutorial to the ColorEasyDuino board:
- VCC → 5 V
- GND → GND
- TXD → D2 (the sketch’s receive pin)
- RXD → D3 (the sketch’s transmit pin)
If you use a different HC-05 breakout, check its supply and signal-level requirements before connecting it.
Upload the bridge sketch
Connect the board over USB without holding the module’s button, so the HC-05 starts in data mode. Paste the sketch into the Arduino IDE, select your board and port, then click Upload. The sketch uses 9600 baud for both Serial Monitor and the HC-05 data connection.
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 11, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include <SoftwareSerial.h>
// Pin 2 is RX, connected to HC-05's TXD
// Pin 3 is TX, connected to HC-05's RXD
SoftwareSerial BT(2, 3);
char val;
void setup() {
pinMode(2,INPUT);
pinMode(3,OUTPUT);
Serial.begin(9600);
Serial.println("BT is ready!");
BT.begin(9600);
}
void loop() {
//Send data from the serial monitor to the mobile app via Bluetooth
if (Serial.available()) {
val = Serial.read();
BT.print(val);
}
//Send data received from the Bluetooth app to the serial monitor
if (BT.available()) {
val = BT.read();
Serial.print(val);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Test the Bluetooth link
- Open Serial Monitor at 9600 baud. Pair the phone with HC-05 if needed, then connect to it from the Bluetooth serial app.
- Send
hello lckfbfrom the phone. Check that it appears in Serial Monitor. - Send
hellofrom Serial Monitor. Check that it appears in the phone app.

Phone app: send hello lckfb and check for hello from Serial Monitor.
5.7.4 SG90 servo
Parts and downloads
Purchase Link: https://detail.tmall.com/item.htm?abbucket=0&id=615779197448&ns=1&spm=a21n57.1.0.0.6f6f523cXTHwFh
Baidu Netdisk download link: https://pan.baidu.com/s/1QsTIKnoQsOTCkeYLLTTjTA?pwd=8889 Password:8889
Specs
Operating Voltage: 3V ~ 7.2V Driving Current: Working Torque: 1.6KG/CM Control Method: PWM Rotation Angle: 180 degrees
Choose a servo
Use a 180° positional servo for this example. On a 360° continuous-rotation servo, the command controls speed and direction instead of angle.
Allow time for the servo to move before sending a new position. The listed speeds are 0.18 or 0.22 seconds per 60°.
Wiring

- Connect the signal wire (usually orange or yellow) to D9, the pin used by the sketch.
- The power wire (red) connects to the 5V pin on the development board.
- The ground wire (brown or black) connects to the GND pin on the development board.
Upload the sweep sketch
Paste this sketch into the Arduino IDE, select your board and port, then click Upload. The sketch uses the Servo library and sends position commands on D9.
#include <Servo.h>
Servo myservo; // Create a servo object to control the SG90
void setup() {
myservo.attach(9); // Connect the servo's signal wire to digital pin 9
}
void loop() {
for (int pos = 0; pos <= 180; pos += 1) {
myservo.write(pos); // Rotate from 0 degrees to 180 degrees
delay(15); // Allow time for the servo to reach the new position
}
for (int pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos); // Rotate back from 180 degrees to 0 degrees
delay(15); // Allow time for the servo to reach the new position
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Check the sweep
After uploading, watch the servo arm sweep back and forth. The sketch sends position commands from 0° to 180° and back, with a 15 ms pause between steps.
See the module manual for more details.