This project demonstrates how to control an Arduino Uno's digital pins (LEDs) from a simple React web interface using a Node.js server as a serial communication bridge.
The setup involves three distinct components communicating over two separate channels:
- React GUI
↔️ (WebSockets)↔️ Node.js Server - Node.js Server
↔️ (Serial Port)↔️ Arduino Uno
The repository is organized into three main sub-directories:
arduino-web-controller/
├── arduino-gui/ \# React Frontend (Web Controller)
├── arduino-serial-server/ \# Node.js Serial Bridge (Middleware)
└── arduino-code/ \# Arduino C++ Sketch (PlatformIO)
To run this project, you need the following installed:
- Arduino IDE or PlatformIO (recommended): For uploading the sketch.
- Node.js (LTS version): For running the server and the React application.
- USB Cable: To connect the Arduino Uno to your computer.
Follow these three steps to get the controller running:
- Open the
arduino-code/folder in VS Code with the PlatformIO extension installed. - Review the file
arduino-code/src/main.cpp. It defines the LED pins (10, 9, 8) and listens for single characters:'r','y', or'g'to toggle the corresponding LED. - Upload the sketch to your Arduino Uno board.
The server acts as the middleman, linking the web (WebSockets) to the hardware (Serial Port).
- Navigate to the
arduino-serial-server/directory:cd arduino-serial-server - Install dependencies:
npm install
- Crucially, open
server.jsand change theARDUINO_PORTvariable to match the port your Arduino is currently using (e.g.,'COM3'on Windows, or'/dev/ttyACM0'on Linux/Mac). - Start the server. Keep this terminal window open.
The server will start on port
node server.js
3001.
The frontend provides the buttons and the serial output monitor.
- Navigate to the
arduino-gui/directory:cd ../arduino-gui - Install dependencies:
npm install
- Start the React development server:
This will open the web controller in your browser (usually at
npm start
http://localhost:3000).
- With all three components running (Arduino, Node.js Server, and React App), click the Toggle Red (R), Toggle Yellow (Y), or Toggle Green (G) buttons.
- The React app sends the corresponding character (
'r','y', or'g') to the Node.js server via WebSocket. - The Node.js server writes the character to the Arduino's USB Serial Port.
- The Arduino toggles the LED and sends a status message back via the Serial Port (e.g., "Red LED Toggled. State: ON").
- The Node.js server receives this status and broadcasts it back to the React app via WebSocket.
- The React app displays the message in the Serial Output box.
| Issue | Potential Solution |
|---|---|
| "Error opening serial port" | Check server.js: Ensure the ARDUINO_PORT matches the port listed in the Arduino IDE/PlatformIO. |
| "Server Status: Disconnected" | Make sure the node server.js terminal is running and did not crash. Check console errors for port conflicts (the server runs on 3001). |
| Arduino not receiving command | Ensure the Baud Rate (9600) is consistent in both the Arduino code (Serial.begin(9600);) and the Node.js server.js file. |
node-serialport installation error |
On some systems (especially Linux/Mac), you may need to install necessary build tools. Try npm install --build-from-source. |