Skip to content

10. Output Devices

Group assignment:

  • Measure the power consumption of an output device.
  • Document your work on the group work page and reflect on your individual page what you learned.

Individual assignment:

Add an output device to a microcontroller board you’ve designed and program it to do something.


Hero Shot

Group assignment

  • This week we tested simple output sensors - LED lights and motors (fans).

  • In the Arduino IDE, open the file - Examples - Analog - Fading.This code is an Arduino PWM (Pulse Width Modulation) dimming program that controls LED lights to achieve fade In and fade Out breathing light effects.

Analog_Fading.png

int ledPin = 9;  // LED connected to digital pin 9

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);
  }
}
  • This time, we will use a digital display tester to observe the voltage and current of the LED. During the process of the LED light turning on and off, we can see that the current value varies between 0.05A-0.06A.
  • Next, we added a mini motor between GNG and VCC and observed a change in current from 0.06A to 0.13A.

OUTPUT_LED_1.jpg


Individual assignment:

For my final project, I need a lighting module and a sound-playing module. I have chosen WS2812 lights, the DFrobot MP3 module (DFR0534), and a 4Ω 3W speaker.
Output_device.png

1. Neopixels WS2812

1.1 The principle of WS2812 LED lights

  • WS2812 is a smart lamp bead with integrated control circuit and RGB three-color LED,
  • which supports single-wire serial communication (only 1 data pin can control multiple lamp beads).
  • Each LED can be independently programmed to achieve rich lighting effects (such as gradients,
  • running water lights, etc.), and is widely used in decoration, display screens, DIY projects and other scenarios.

ws2812_0.jpg

  • core characteristic Single-wire control: multiple lamp beads are connected in series,
    and only 1 data line is required for control.

ws2812_2.jpg

  • 24-bit color: Each LED can display 160,000 colors (8 bits each of red, green, and blue).

ws2812.jpg

  • Cascade function: The data is automatically transmitted downwards,
    and the number of lamp beads can be theoretically expanded infinitely (limited by refresh rate and power supply).

ws2812.jpg

  • Voltage range: Usually 5V powered (some models support 3.5 V-5.3 V).

1.2 WS2812 LED Implementation Guide

1. Power Supply Issues

  • Current Requirements : A single WS2812 LED draws approximately 60mA when displaying full white. For 10 LEDs, ensure a power supply with at least 600mA capacity.

  • Power Source Selection :Avoid powering LEDs directly from a microcontroller (insufficient current). Use an external independent power supply and verify its total power rating.

  • Voltage Matching: 5V LEDs require a 5V power source. For 3.3V microcontrollers (e.g., ESP32), consider adding a logic level converter for signal compatibility.

2. Signal Interference

  • Data Line Length :Excessively long data lines (>0.5 meters) may cause signal distortion.

  • Wiring Sequence: Data flows from DI (Data Input) → DO (Data Output).Reversing the order will disable the entire LED strip.

3. Coding & Libraries

  • Timing Sensitivity :WS2812 requires precise timing control.Always use dedicated libraries like FastLED or NeoPixel (for Arduino).

  • Avoid Blocking Code:Replace delay() with non-blocking methods (e.g., millis()) for smooth animations.

4. Static & Heat Management

  • Heat Dissipation :Prolonged high-brightness use may cause overheating. Add heat sinks or reduce brightness.

5. Soldering & Wiring

  • Soldering Temperature:For surface-mount WS2812 LEDs, keep soldering temperature ≤300°C and duration ≤3 seconds to avoid damage.

  • Parallel Power Supply:For long LED strips, power both ends to minimize voltage drop.

6. Debugging Tips

  • Segment Testing :Start with a small group (3–5 LEDs) to verify code/hardware before expanding.

  • Common Ground : Connect the microcontroller and LED strip to the same GND to prevent voltage mismatches.

1.3 Connect WS2812

  • Hardware connection: The circuit board I designed for the final project has a total of three pins that can be connected to WS2812, namely D1/D3/D5. For the first test, I connected it to pin D5.
    WS2812_connect.jpg

1.4 Programming WS2812 RGB Lights

  • In the Arduino IDE, click Tools> Manage Libraries > search for Adafruit_NeoPixel and install the latest version.
    FASTLEDlibrary
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define PIN       D5 // On Trinket or Gemma, suggest changing this to 1
#define NUMPIXELS 64 // Popular NeoPixel ring size
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 100 // Time (in milliseconds) to pause between pixels

void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  clock_prescale_set(clock_div_1);
#endif

  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}

void loop() {
  pixels.clear(); // Set all pixel colors to 'off'
  for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
    pixels.setPixelColor(i, pixels.Color(0, 150, 0));

    pixels.show();   // Send the updated pixel colors to the hardware.

    delay(DELAYVAL); // Pause before next pass through loop
  }
} 

2. Speaker

  • The speaker converts varying electrical current signals into mechanical vibrations through the principle of electromagnetic induction, ultimately generating sound.
    speaker_principle.gif

  • For this test, I used speakers with 4 Ω 3W and 8 Ω 2W.
    speaker.jpg

3. MP3 module (DFR0534)

3.1 Introduction to DFR0534

  • About DFR0534

  • The MP3 voice module has a built-in 8MB storage space, which can store over 200 sound effects for selection.

  • The storage method is as simple as using a USB flash drive, and the sound effects inside the module can be updated at any time.
  • Supports MP3 and WAV audio formats, can be used for specified playback, loop playback, 30 level volume adjustment, etc.

DFR0534.jpg

3.2 parameter information

  • technical specifications

  • Working Voltage: 3.3V–5V

  • Interface Type: UART
  • Supports MP3/WAV hardware decoding
  • Supported Sample Rates (KHz): 8/11.025/12/16/22.05/24/32/44.1/48
  • Supports SPI Flash emulation as a USB drive – Allows direct updates to voice files in SPI Flash like a USB drive
  • 30-level volume adjustment
  • Module Dimensions: 22×30mm

  • pinouts

No. Name Description
1 T TX (Transmit Data)
2 R RX (Receive Data)
3 - Ground (GND)
4 + Power Positive (3.3V-5V)
5 VCC Power Positive (3.3V-5V)
6 GND Ground (GND)
7 BUSY Busy signal (High during playback)
8 SP+ Speaker positive terminal
9 SP- Speaker negative terminal
10 DACL DAC left channel output
11 DACR DAC right channel output
12 ONE 1-Wire serial control
13 Micro USB Audio update interface (U盘模式)

3.3 DFR0534 to XIAO-ESP32-C3 Connection

DFR0534 Pin XIAO-ESP32-C3 Pin
VCC 3.3V
GND GND
RX D6 (GPIO6)
TX D7 (GPIO7)

3.4 DFR0534 code

DFR0534 Random Audio Player: I sought the help of DEEPSEEK for this code: “The RX of XIAO-ESP32-C3 is D6, and the TX is D7. Let DFR0534 connect to XIAO-ESP32-C3 and randomly play 01-03 in the memory MP3 files, play only one clip at a time “.

#define TX_PIN D6  // Connect to module's RX
#define RX_PIN D7  // Connect to module's TX

// Configuration
const int trackDuration = 1000;  // Play duration for each track (ms)

void setup() {
  Serial1.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN); // Initialize UART
  randomSeed(analogRead(A0)); // Seed random generator
  setVolume(0x1E); // Set medium volume (0x00-0x1E)
}

void loop() {
  // Randomly select track (1=01.mp3, 2=02.mp3, 3=03.mp3)
  byte track = random(1, 4); 

  // Play selected track
  playTrack(track);

  // Wait for track to play
  delay(trackDuration); 
}

/**
 * Plays specified audio track
 * @param trackNum - Track number (1-3)
 */
void playTrack(unsigned char trackNum) {
  // Command format: [Header, Command, Len, Param1, TrackNum, Checksum]
  unsigned char cmd[6] = {0xAA, 0x07, 0x02, 0x00, trackNum, trackNum + 0xB3};
  Serial1.write(cmd, 6);
}

/**
 * Sets playback volume
 * @param vol - Volume level (0x00=mute, 0x1E=max)
 */
void setVolume(unsigned char vol) {
  unsigned char cmd[5] = {0xAA, 0x13, 0x01, vol, vol + 0xBE};
  Serial1.write(cmd, 5);
} 

FILES

rgb_code.ino
DF_MP3.ino