Blink

The LED Blink application is a project that allows the user to control the blinking of an LED using an SMD and an RGB LED Module. This project is designed for simplicity, making it an ideal starting point for those wishing to experience the SMD hardware control using the SMD libraries.

"Blink" Project

About Tools and Materials:

SMD Red (Purchase Here)

SMD USB Gateway (Purchase Here)

Arduino Gateway Module (Purchase Here)

RGB LED Module (Purchase Here)

Step 1: Hardware & Software Overview

Project Key Components

  • SMD

    The SMD acts as a bridge between the script and the RGB LED Module. It is responsible for interpreting the commands sent by the script and translating them into actions that toggle the RGB module.

  • RGB LED Module

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

  • 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 turn the RGB LED on or off, adjust the color, or set a specific blinking pattern.

Project Key Features

  • Detailed Control over the LED

    The user can easily control the LED of the RGB LED Module colors by simply using the necessary function of SMD libraries. The LED on the module can emit all RGB color values.

Step 2: Assemble

  1. Hardware Setup

Project Wiring Diagram

Step 3: Run & Test

  1. Run the Script

    • Run the script on your computer. This will establish communication with the SMD and initiate control of the RGB LED Module.

  2. Experience and Customize:

    • Explore different blinking patterns, change the color of the RGB LED to suit your preferences.

Codes

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

def USB_Port():
    """
    Detects the USB serial port based on the operating system.
    Returns the detected port or None if no suitable port is found.
    """
    ports = list(comports())
    usb_names = {
        "Windows": ["USB Serial Port"],  # Serial port names specific to Windows
        "Linux": ["/dev/ttyUSB"],        # Serial port names specific to Linux
        "Darwin": [                      # Serial port names specific to macOS
            "/dev/tty.usbserial",
            "/dev/tty.usbmodem",
            "/dev/tty.SLAB_USBtoUART",
            "/dev/tty.wchusbserial",
            "/dev/cu.usbserial",
        ]
    }
    
    # Get the operating system name
    os_name = system()
    if ports:
        for port, desc, hwid in sorted(ports):
            # Check if any known USB name matches the port or its description
            if any(name in port or name in desc for name in usb_names.get(os_name, [])):
                print("Connected to:", port)
                return port
        # If no suitable port is found, display all available ports
        print("Available ports:")
        for port, desc, hwid in ports:
            print(f"Port: {port}, Description: {desc}, HWID: {hwid}")
    else:
        print("No ports detected!")
    return None

# Detect the serial port dynamically
SerialPort = USB_Port()
if not SerialPort:
    print("No suitable USB port found! Exiting...")
    exit(1)

# Define constants
baudrate = 115200       # Baud rate for communication
ID = 0                  # ID of the SMD
rgb_module_id = 5       # ID of the RGB LED module

try:
    # Initialize the Master module for communication with the SMD
    master = Master(SerialPort, baudrate)       # Sets up the USB gateway module
    master.attach(Red(ID))                      # Connects to the SMD with the specified ID
    master.scan_modules(ID)                     # Scans and identifies connected modules to the SMD

    # RGB LED control loop
    while True:
        # Set the RGB LED to red
        master.set_rgb(ID, rgb_module_id, 255, 0, 0)        # RGB values: 255 (Red), 0 (Green), 0 (Blue)
        time.sleep(0.5)                                     # Delay to control blinking frequency
        # Turn off the RGB LED
        master.set_rgb(ID, rgb_module_id, 0, 0, 0)          # RGB values: 0 (All colors off)
        time.sleep(0.5)                                     # Delay to control blinking frequency

except FileNotFoundError as e:
    # Handle the case when the serial port is not found
    print(f"Error: {e}")
    print("Make sure the specified serial port exists and is connected.")
except Exception as e:
    # Handle any unexpected errors
    print(f"Unexpected error: {e}")                                    # Value can be changed to see how the blinking frequency changes

Last updated