Skip to content

adamlopardo/arduino-home-automation

Repository files navigation

Arduino Nano RP2040 Home Automation System

A complete home automation controller for Arduino Nano RP2040 with serial control from your MacBook.

Features

  • Digital Control: Control relays, lights, and other on/off devices
  • PWM Control: Dimming lights, motor speed control, etc. (0-255 range)
  • Sensor Reading: Read digital and analog sensors
  • Serial Interface: Control via Serial Monitor or Python script
  • Status Monitoring: Real-time status reporting
  • MacBook Integration: Python control script included

Hardware Setup

Pin Configuration

Digital Output Pins (Relays/Switches):

  • Pin 2, 3, 4, 5 - Configured as relay outputs

PWM Output Pins (Dimmable):

  • Pin 6, 9, 10 - PWM-capable outputs (0-255)

Analog Input Pins (Sensors):

  • A0, A1, A2 - Analog sensor inputs (0-3.3V)

Wiring Examples

Controlling a Relay Module

Arduino Pin 2 → Relay Module IN1
Arduino GND   → Relay Module GND
Arduino 3.3V  → Relay Module VCC

Connecting an LED (with resistor)

Arduino Pin 6 → 220Ω Resistor → LED Anode (+)
LED Cathode (-) → Arduino GND

Connecting a Temperature Sensor (analog)

Sensor VCC → Arduino 3.3V
Sensor GND → Arduino GND
Sensor OUT → Arduino A0

Software Setup

1. Arduino IDE Setup

  1. Install Arduino IDE (if not already installed):

  2. Install RP2040 Board Support:

    • Open Arduino IDE
    • Go to Arduino IDE → Settings (or Preferences)
    • In "Additional Board Manager URLs", add:
      https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
      
    • Go to Tools → Board → Board Manager
    • Search for "RP2040"
    • Install "Raspberry Pi Pico/RP2040" by Earle F. Philhower, III
  3. Configure Board Settings:

    • Go to Tools → Board → Raspberry Pi RP2040 Boards
    • Select "Arduino Nano RP2040 Connect"
    • Select your port: Tools → Port → /dev/cu.usbmodem*

2. Upload the Arduino Sketch

  1. Open home_automation.ino in Arduino IDE
  2. Click the Upload button (→)
  3. Wait for compilation and upload to complete
  4. Open Serial Monitor: Tools → Serial Monitor
  5. Set baud rate to 115200
  6. You should see the startup message!

3. Test via Serial Monitor

Try these commands in the Serial Monitor:

HELP          # Show all commands
STATUS        # Show current pin states
ON:2          # Turn on pin 2
OFF:2         # Turn off pin 2
PWM:6:128     # Set pin 6 to 50% (128/255)
ANALOG:A0     # Read analog pin A0

Using the Python Control Script

Install Requirements

Open Terminal and run:

pip3 install pyserial

Run the Controller

python3 arduino_control.py

The script will automatically find your Arduino and provide an interactive menu.

Python Script Features

  • Auto-detection: Automatically finds Arduino port
  • Interactive Menu: Easy-to-use text interface
  • Real-time Control: Instant feedback from Arduino
  • Status Monitoring: View all pin states

Command Reference

Digital Control

ON:<pin>           # Turn pin ON
OFF:<pin>          # Turn pin OFF
TOGGLE:<pin>       # Toggle pin state

Examples:
  ON:2             # Turn on relay on pin 2
  OFF:3            # Turn off relay on pin 3
  TOGGLE:4         # Toggle relay on pin 4

PWM Control

PWM:<pin>:<value>  # Set PWM value (0-255)

Examples:
  PWM:6:255        # Pin 6 full brightness
  PWM:9:128        # Pin 9 at 50%
  PWM:10:0         # Pin 10 off

Reading Pins

READ:<pin>         # Read digital pin state
ANALOG:<pin>       # Read analog value

Examples:
  READ:2           # Read state of pin 2
  ANALOG:A0        # Read analog sensor on A0

System Commands

STATUS             # Show all pin states and values
AUTOREPORT:ON      # Enable automatic status reports (every 5s)
AUTOREPORT:OFF     # Disable automatic reports
RESET              # Turn off all outputs
HELP               # Show command help

Practical Use Cases

1. Smart Lighting

  • Connect relay to pin 2 for main lights
  • Connect LED strip with PWM to pin 6 for dimmable accent lighting
  • Control via Python script or schedule commands

2. Temperature Monitoring

  • Connect temperature sensor to A0
  • Use ANALOG:A0 to read values
  • Convert readings to temperature in Python script

3. Fan Control

  • Connect relay to pin 3 for on/off control
  • Or use PWM on pin 9 for variable speed

4. Security System

  • Use digital inputs to read door sensors
  • Control alarm outputs with relays
  • Monitor status continuously

Customization

Adding More Pins

Edit the pin arrays in the Arduino sketch:

const int RELAY_PINS[] = {2, 3, 4, 5, 7, 8};  // Add more pins
const int NUM_RELAYS = 6;  // Update count

Changing Serial Baud Rate

In both files:

  • Arduino: Serial.begin(115200);
  • Python: baudrate=115200

Adding Automation Logic

Add to the loop() function:

void loop() {
  // Your automation logic
  if (analogRead(A0) > 500) {
    digitalWrite(2, HIGH);  // Turn on fan if hot
  }
  
  // ... existing code ...
}

Troubleshooting

Arduino Not Found

  1. Make sure USB cable is connected
  2. Check Tools → Port in Arduino IDE
  3. Try unplugging and replugging the Arduino
  4. On Mac, look for /dev/cu.usbmodem* ports

Upload Fails

  1. Hold down the BOOTSEL button while connecting USB
  2. Release after it appears as a drive
  3. Try uploading again

Serial Monitor Shows Garbage

  • Check baud rate is set to 115200

Python Script Can't Find Arduino

  1. List ports: python3 -m serial.tools.list_ports
  2. Manually specify port in script
  3. Check permissions: sudo chmod 666 /dev/cu.usbmodem*

No Response from Arduino

  1. Ensure Serial Monitor is closed (only one program can use serial port)
  2. Wait 2 seconds after connecting for Arduino to reset
  3. Try pressing the reset button on Arduino

Safety Notes

⚠️ Important Safety Information:

  • Voltage Limits: Arduino pins are 3.3V. Do NOT connect 5V devices directly.
  • Current Limits: GPIO pins max 12mA. Use relays or transistors for high-current loads.
  • Relay Ratings: Ensure relays are rated for your AC/DC voltage and current.
  • Isolation: Use optically isolated relays for AC mains control.
  • Fusing: Add fuses to circuits controlling high-power devices.
  • Testing: Test all circuits with low power before connecting to mains.

Advanced Integration

Home Assistant Integration

The serial protocol can be integrated with Home Assistant using the Serial integration.

MQTT Bridge

Create a Python script to bridge serial commands to MQTT for IoT integration.

Web Interface

Add a Flask web server to the Python script for browser-based control.

License

This project is open source. Feel free to modify and share!

Support

For Arduino Nano RP2040 documentation:

For issues or questions:

  • Check the troubleshooting section above
  • Review Arduino IDE error messages
  • Test with simple commands in Serial Monitor first

Created for: MacBook Air with Arduino IDE
Board: Arduino Nano RP2040 Connect
Communication: USB Serial (115200 baud)

About

Arduino Nano RP2040 Home Automation Controller

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published