Experiment 8: Optocoupler Relay Experiment

From Diustou Wiki

接口说明

  • VCC: Positive terminal for DC power supply of the module
  • GND: Negative terminal for DC power supply of the module
  • IN: Relay signal trigger terminal (can be set to high or low level to control relay activation)
  • Relay Trigger Mode Selection Terminal: When the DIP switch is set to L, it is triggered by a low level; when set to H, it is triggered by a high level.
    • NO: Normally open interface of the relay, open before the relay is activated, and short-circuited to COM when activated
    • COM: Common interface of the relay
    • NC: Normally closed interface of the relay, short-circuited to COM before the relay is activated, and open when activated


Arduino

Experimental Phenomenon

  • The relay is energized once every second, and simultaneously, the LED indicator on the module lights up every second.

Circuit Connection

  • 实验八:光耦继电器实验 接线1.png

Reference Program

int relay = 2; // Relay activation trigger signal - high level active;
void setup()
{
 pinMode(relay,OUTPUT); // Define port as output;
}
void loop()
{
 digitalWrite(relay,HIGH); // Activate relay;
 delay(1000);
 digitalWrite(relay,LOW); // Deactivate relay;
 delay(1000);
}

Raspberry Pi

Circuit Connection

  • 实验八:光耦继电器实验 接线2.png

Program Execution

Python

  • Install the gpiozero library
    • You can install the library using the following commands:
sudo apt update
sudo apt install python3-gpiozero
  • For other systems on the Raspberry Pi, you can install the library using:
sudo pip3 install gpiozero
  • Run the following command to view the GPIO pin definitions on the Raspberry Pi:
pinout
  • Download the Raspberry Pi reference examples, unzip the files, copy them to your user directory, and run:
cd raspberrypi/8/python_gpiozero
python relay.py
  • You should now see the Raspberry Pi running the optocoupler relay program correctly. To exit, press ctrl+C.
  • Command explanation: gpiozero.LED(pin, pwm, active_high, initial_value)
    • Main parameters:
      • pin: GPIO pin number,
      • active_high:
        • When set to True (default), connect the LED's negative terminal to GND and the other terminal to the GPIO pin.
        • When set to False, connect the LED's negative terminal to the GPIO pin and the other terminal to the 3.3V pin.
      • initial_value:
        • If False (default), the LED initial state is off.
        • If None, the LED pin is in an unstable state.
        • If True, the LED initial state is on.
  • For more commands, please refer to the gpiozero documentation

Example Download

Related Examples

Basic Experiment Kits and Examples for Arduino and Raspberry Pi
教程名2
  • 列表2
教程名3
  • 列表3
教程名4
  • 列表4
教程名5
  • 列表5
教程名6
  • 列表6