USB Power Module (Relay Control)
This guide explains how to control a USB Power module with SparkC3.
The same approach also applies to commercially available relay control modules.
Schematic

Hardware Connections
Connect SparkC3 to the USB Power module and check the wiring carefully.

Basic Code Example
Arduino IDE Example
Use SparkC3 to switch the relay module on and off at two-second intervals.
Use the tool below to flash precompiled firmware to SparkC3 directly in your browser, without installing any software:
ESP32 Flasher (Official API)
Select firmware
Operation log
Instructions and troubleshooting
If flashing fails or you encounter other issues, see the ESP Flasher guide
Code:
#include <Arduino.h>
#define POWER_PIN 9 // USB Power control pin
void setup() {
Serial.begin(115200);
pinMode(POWER_PIN, OUTPUT);
digitalWrite(POWER_PIN, HIGH); // Set HIGH to turn USB power off
Serial.println("USB Power control system started");
}
void loop() {
// Turn USB Power on
digitalWrite(POWER_PIN, LOW);
Serial.println("USB Power on");
delay(2000);
// Turn USB Power off
digitalWrite(POWER_PIN, HIGH);
Serial.println("USB Power off");
delay(2000);
}MicroPython Example
Coming soon...
Remote Control over Wi-Fi
Control USB Power remotely over Wi-Fi with the following features:
- Preconfigured USB Power control code
- Wi-Fi connectivity and a web server
- A remote control interface
- Status monitoring
Steps
- Connect SparkC3 to your computer using USB Serial.
- Open the code in Arduino IDE.
- Replace the Wi-Fi SSID and password.
- Select the correct serial device in the dialog.
- Upload the code to SparkC3.
- Wait for flashing to finish.
- Connect SparkC3 to the USB Power module.
Code:
#include <WiFi.h>
#include <WebServer.h>
// !!! Replace with your Wi-Fi SSID and password !!!
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";
WebServer server(80);
const int relayPin = 9;
bool relayState = false;
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Initially off
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Wi-Fi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Configure web server routes
server.on("/", handleRoot);
server.on("/relay/on", handleRelayOn);
server.on("/relay/off", handleRelayOff);
server.on("/status", handleStatus);
server.begin();
Serial.println("Web server started");
}
void loop() {
server.handleClient();
}
void handleRoot() {
String html = "<!DOCTYPE html>";
html += "<html><head>";
html += "<meta charset='UTF-8'>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
html += "<title>SparkC3 USB Power Control</title>";
html += "<style>";
html += "body { font-family: Arial, sans-serif; text-align: center; margin: 50px; }";
html += "button { padding: 10px 20px; margin: 10px; font-size: 16px; cursor: pointer; }";
html += ".status { font-size: 18px; margin: 20px 0; }";
html += "</style>";
html += "</head><body>";
html += "<h1>SparkC3 USB Power Control</h1>";
html += "<div class='status'>USB Power status: <span id='status'>" + String(relayState ? "On" : "Off") + "</span></div>";
html += "<button onclick='toggleRelay(true)'>On</button>";
html += "<button onclick='toggleRelay(false)'>Off</button>";
html += "<script>";
html += "function toggleRelay(state) {";
html += " var xhr = new XMLHttpRequest();";
html += " xhr.onreadystatechange = function() {";
html += " if (xhr.readyState == 4 && xhr.status == 200) {";
html += " document.getElementById('status').innerText = state ? 'On' : 'Off';";
html += " }";
html += " };";
html += " xhr.open('GET', state ? '/relay/on' : '/relay/off', true);";
html += " xhr.send();";
html += "}";
html += "</script>";
html += "</body></html>";
server.send(200, "text/html; charset=utf-8", html);
}
void handleRelayOn() {
digitalWrite(relayPin, LOW);
relayState = true;
server.send(200, "text/plain; charset=utf-8", "USB Power is on");
}
void handleRelayOff() {
digitalWrite(relayPin, HIGH);
relayState = false;
server.send(200, "text/plain; charset=utf-8", "USB Power is off");
}
void handleStatus() {
String json = "{\"relay\":" + String(relayState ? "true" : "false") + "}";
server.send(200, "application/json", json);
}Notes
None yet.
Troubleshooting
Common Problems and Solutions
1. Unable to Connect to the Device
Symptoms: Clicking Connect Device does not open the serial port dialog, or the connection fails.
Solutions:
- Make sure you are using Chrome or Edge.
- Check that the USB cable supports data transfer and is not a charge-only cable.
- Try another USB port.
- Check whether the ESP32 device appears in Device Manager.
2. Flashing Fails
Symptoms: Flashing stops unexpectedly or an error message appears.
Solutions:
- Reduce the baud rate to 115200 and try again.
- Check that the firmware file is complete.
- Close other programs that may be using the serial port, such as the Arduino IDE Serial Monitor.
3. The Device Does Not Work
Symptoms: Firmware flashes successfully, but USB Power does not respond.
Solutions:
- Check the hardware connections.
- Make sure the relay module has power.
- Use a serial monitor to inspect the device startup log.
- Check the Wi-Fi connection status.
4. Unable to Access the Web Interface
Symptoms: The device connects to Wi-Fi, but the control page cannot be opened.
Solutions:
- Make sure the device and computer are on the same network.
- Check whether device isolation is enabled on the router.
- Try accessing the device directly using its IP address.
- Check the firewall settings.
Get Help
If none of these steps resolves the problem:
- Record the full error message and the steps you took.
- Contact technical support:
- QQ group: [To be added]
- Email: lbuque@163.com
- GitHub Issues: [To be added]