Node.js Raspberry Pi GPIO Introduction
Here is a clean, simple, and complete guide for Node.js Raspberry Pi GPIO Introduction.
🍓 Node.js Raspberry Pi GPIO Introduction
Raspberry Pi boards come with 40 GPIO (General Purpose Input/Output) pins that allow you to interact with external electronics such as:
LEDs
Buttons
Sensors
Relays
Motors
Displays
With Node.js, you can easily control these GPIO pins using JavaScript, making it perfect for IoT and automation.
⭐ Why Use Node.js for Raspberry Pi GPIO?
✔ Non-blocking, event-driven architecture
✔ Perfect for real-time hardware tasks
✔ Huge ecosystem (npm packages)
✔ Easy to build web dashboards + hardware control
✔ Ideal for IoT, robotics, and home automation
🧩 GPIO Pin Types on Raspberry Pi
Raspberry Pi provides:
🔹 GPIO pins (input/output)
🔹 3.3V power pins
🔹 5V power pins
🔹 Ground pins
🔹 Special pins like I2C, SPI, UART
⚠ Important: GPIO pins operate at 3.3V, NOT 5V.
📦 Popular Node.js GPIO Libraries
| Library | Best For | Notes |
|---|---|---|
| onoff | Basic GPIO | Simple, widely used |
| rpi-gpio | Beginner friendly | Easy API |
| pigpio | Advanced control | PWM, servo control, fast timing |
Most commonly used: onoff
📌 Installing GPIO Library
🔌 Basic GPIO Setup with Node.js
GPIO pins have two modes:
"out"→ output (LED, relay)"in"→ input (button, sensor)
💡 Example 1: Blink an LED (GPIO Output)
Hardware
LED connected to GPIO 17
Add a 330Ω resistor in series
Code
Run:
🎛️ Example 2: Read a Button (GPIO Input)
Hardware
Button connected to GPIO 4
Use pull-down resistor
Code:
🌐 Example 3: Control GPIO from a Browser
You can turn an LED ON/OFF using Node.js + Express.
Now visit:
⚙ GPIO Modes & Events
Modes:
"in"→ read signals"out"→ send signals"high"→ initial high voltage"low"→ initial low voltage
Input edge detection:
"rising"→ low → high"falling"→ high → low"both"→ detect any change
🧠 Best Practices
✔ Use external resistors
✔ Avoid 5V devices directly
✔ Stop your app before rewiring
✔ Use led.unexport() when exiting
✔ Prefer pigpio for fast or PWM-based projects
