Advertisement
microrobotics

BT138 control AC loads

Apr 26th, 2023
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The BT138 is a Triac that can be used to control AC loads, such as an AC lamp, using an Arduino. In this example, I'll provide a code snippet that demonstrates how to control an AC lamp using a BT138 Triac and an Arduino with zero-crossing detection.
  3.  
  4. Hardware Setup:
  5.  
  6. Connect an MOC3021 optocoupler's pin 1 (LED anode) to digital pin 9 on the Arduino.
  7. Connect the MOC3021 optocoupler's pin 2 (LED cathode) to the GND (ground) of the Arduino through a 220-ohm resistor.
  8. Connect the BT138 Triac's Gate (G) to pin 6 (Triac Driver) of the MOC3021 optocoupler.
  9. Connect the BT138 Triac's MT1 to the AC load (lamp) and the Neutral wire of the AC power source.
  10. Connect the BT138 Triac's MT2 to the Live wire of the AC power source.
  11. Connect pin 4 (Triac Driver) of the MOC3021 optocoupler to the Neutral wire of the AC power source.
  12. Note: Working with AC power can be dangerous. Please exercise extreme caution when working with AC voltage, and if you are unsure about anything, consult an experienced professional.
  13.  
  14. This code controls the brightness of an AC lamp connected to a BT138 Triac using an Arduino and an MOC3021 optocoupler. The brightness is determined by the dimming variable, which can be set to a value between 0 (off) and 128 (fully on). The zero-crossing detection is achieved by delaying the Arduino for a fixed period (65 microseconds), which corresponds to the zero-crossing interval for a 60 Hz AC power source.
  15.  
  16. Important: This example assumes a 60 Hz AC power source. If you are using a 50 Hz AC power source, change the delayMicroseconds(65) line to delayMicroseconds(100).
  17.  
  18. Code:
  19. */
  20.  
  21. const int lampPin = 9; // Digital pin 9 connected to the anode of MOC3021 optocoupler's LED
  22. const int dimming = 128; // Dimming level (0-128), 0 = off, 128 = fully on
  23.  
  24. void setup() {
  25.   pinMode(lampPin, OUTPUT); // Set the lamp pin as an output
  26. }
  27.  
  28. void loop() {
  29.   for (int i = 0; i < 128; i++) {
  30.     delayMicroseconds(65); // Wait for zero-crossing (8.33ms for 60Hz, 10ms for 50Hz)
  31.     if (i < dimming) {
  32.       digitalWrite(lampPin, HIGH); // Turn on the lamp
  33.       delayMicroseconds(10); // Optocoupler LED on-time
  34.       digitalWrite(lampPin, LOW); // Turn off the lamp
  35.     }
  36.   }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement