Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- Hardware Setup:
- Connect an MOC3021 optocoupler's pin 1 (LED anode) to digital pin 9 on the Arduino.
- Connect the MOC3021 optocoupler's pin 2 (LED cathode) to the GND (ground) of the Arduino through a 220-ohm resistor.
- Connect the BT138 Triac's Gate (G) to pin 6 (Triac Driver) of the MOC3021 optocoupler.
- Connect the BT138 Triac's MT1 to the AC load (lamp) and the Neutral wire of the AC power source.
- Connect the BT138 Triac's MT2 to the Live wire of the AC power source.
- Connect pin 4 (Triac Driver) of the MOC3021 optocoupler to the Neutral wire of the AC power source.
- 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.
- 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.
- 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).
- Code:
- */
- const int lampPin = 9; // Digital pin 9 connected to the anode of MOC3021 optocoupler's LED
- const int dimming = 128; // Dimming level (0-128), 0 = off, 128 = fully on
- void setup() {
- pinMode(lampPin, OUTPUT); // Set the lamp pin as an output
- }
- void loop() {
- for (int i = 0; i < 128; i++) {
- delayMicroseconds(65); // Wait for zero-crossing (8.33ms for 60Hz, 10ms for 50Hz)
- if (i < dimming) {
- digitalWrite(lampPin, HIGH); // Turn on the lamp
- delayMicroseconds(10); // Optocoupler LED on-time
- digitalWrite(lampPin, LOW); // Turn off the lamp
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement