Autonomous Lighting

The autonomous lighting project is a basic environmental interaction example. It imitates a night lamp, using the Ambient Light Sensor Module to sense the light level and let the user decide what level is dark enough to turn on the RGB LED Module. It also helps with energy efficiency by turning the LED when it is bright enough.

About Tools and Materials:

SMD Red (Purchase Here)

SMD USB Gateway (Purchase Here)

Arduino Gateway Module (Purchase Here)

RGB LED Module (Purchase Here)

Ambient Light Sensor Module (Purchase Here)

Step 1: Hardware & Software Overview

Project Key Components

  1. SMD

    The SMD acts as a bridge between the script and the modules. It is responsible for interpreting the commands sent by the script and translating them into actions that read input from the Ambient Light Sensor Module and toggle the RGB LED Module.

  2. RGB LED Module

    The RGB LED Module is designed to emit different colors, allowing users to experiment with different lighting effects.

  3. Ambient Light Sensor Module

    The Ambient Light Sensor Module is measures the surrounding light intensity and provides feedback of the intensity to the script, allowing the user to decide whether to turn on or off the LED.

  4. SMD Libraries

    The SMD library is at the heart of the application. It communicates with the SMD using a specific communication protocol, sending commands to read the Ambient Light Sensor Module and toggle the LED on the RGB LED Module.

Project Key Features

  • Automatic Light Sensing

    The Ambient Light Sensor Module is a input module and the bridge between the physical world and the script, allowing the user to read the data of the environment.

  • Energy-Efficient Operation

    The Ambient Light Sensor Module allows the user to only turn on the light when it is dark and needed, thus, it helps with the energy efficiency.

  • Real-time Monitoring

    The script can also be modified to monitor the ambient light intensity in real-time.

Step 2: Assemble

Getting Started

  1. Hardware Setup

Project Wiring Diagram

Step 3: Run & Test

  1. Run the Script

  2. Observe Autonomous Operation

    • Observe how the LED automatically turns on when the environment becomes dark and turns off when there is sufficient ambient light.

Codes

from serial.tools.list_ports import comports
from platform import system
from smd.red import *

baudrate = 115200       # Baud rate of communication
ID = 0                  # ID of the SMD
rgb_module_id = 5       # ID of the RGB LED module
ambient_module_id = 5   # ID of the ambient light sensor module


def USB_Port():
    """
    Scans and identifies a compatible USB port for the current operating system.

    Returns:
        str: The detected USB port or None if no suitable port is found.
    """
    # Get a list of available ports
    ports = list(comports())
    
    # Known USB port names for different operating systems
    usb_names = {
        "Windows": ["USB Serial Port"],  # Names specific to Windows
        "Linux": ["/dev/ttyUSB"],        # Names specific to Linux
        "Darwin": [                      # Names specific to macOS
            "/dev/tty.usbserial",
            "/dev/tty.usbmodem",
            "/dev/tty.SLAB_USBtoUART",
            "/dev/tty.wchusbserial",
            "/dev/cu.usbserial",
        ]
    }

    # Detect the operating system
    os_name = system()
    print(f"Operating System: {os_name}")

    if ports:
        for port in ports:
            # Check if the port matches any known USB names
            if any(name in port.device or name in port.description for name in usb_names.get(os_name, [])):
                print(f"USB device detected on port: {port.device}")
                return port.device  # Return the first matching port
        # If no suitable port is found, print the list of available ports
        print("No suitable USB device found. Available ports:")
        for port in ports:
            print(f"Port: {port.device}, Description: {port.description}, HWID: {port.hwid}")
    else:
        print("No ports detected!")
    return None


try:
    # Find a valid serial port
    SerialPort = USB_Port()
    if not SerialPort:
        raise Exception("No compatible USB port found. Please check your connection.")

    print(f"Using serial port: {SerialPort}")

    # Initialize the SMD module
    master = Master(SerialPort, baudrate)       # Defines the USB gateway module
    master.attach(Red(ID))                      # Gives access to the SMD of specified ID
    master.scan_modules(ID)                     # Scans and identifies the modules connected to the SMD

    # Main loop
    while True:
        # Get ambient light data from the sensor
        light = master.get_light(ID, ambient_module_id)  # Variable to store the ambient light data
        print(f"Ambient light level: {light}")           # Print the value to observe

        if light < 30:
            # Set the RGB LED to white
            master.set_rgb(ID, rgb_module_id, 255, 255, 255)  # RGB values for white
        else:
            # Turn off the RGB LED
            master.set_rgb(ID, rgb_module_id, 0, 0, 0)        # RGB values for off

except Exception as e:
    print(f"Error: {e}")

Last updated