I have a Leading edge triac Ac Dimmer. I am looking for some code to operate a beacon light.
I need the light to ramp up from 40% power up to 100% power. When it hits 100% power it stays in that state for 5 seconds and then it ramps back down to 40% power and loops.
Does anyone have code that I can use for this project?
The Triac AC Dimmer has inputs GND, GND, PWM and VCC and it can dim one light bulb.
@ramiro83, your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem
Please edit your post, select all code and click the </> button to apply so-called code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.
To my knowledge, all lightbulb dimmers work with zero-cross detection; you will have to build / buy that.
What im looking to do is in the "Serial Monitor" I would like to see a real-time read out of the current power level. I got the print statement but im missing the function to pull the current power level.
Can you assist?
int led = 9; // the pin that the LED is attached to
int brightnesslow = 0; // low power setting of the LED
int brightnesshigh = 255; // high power setting of the LED
int fadeAmount = 2; // how many points to fade the LED by
int power = 0;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println(F("Initialize System"));
}
void loop() {
for(power=brightnesslow;power<=brightnesshigh;power+=fadeAmount){
analogWrite(led,brightnesslow);
brightnesslow = brightnesslow - fadeAmount;
Serial.print("lampValue -> ");
Serial.println("%");
delay(30);}
}
I think that you want to be using the power level (0>255) as the value for analogWrite()
analogWrite(led,power);
What is this line supposed to do? brightnesslow = brightnesslow - fadeAmount;
brightnesslow will become a negative number and the led will not illuminate the second pass through the for loop.
You need to review your math for how to determine the power percentage.
The formula for percentage is (value/totalvalue)*100.
In your case it will be (power/maxbrightness)*100.
Because the division of power/maxbrightness will be less than 1 and will become 0 when using integer values it is necessary to multiply power by 100 before the division.
Thank you so much Cattledog for helping me out. I do appreciate you very much. The issue im having now is that the incandescent bulb i have connected to the dimmer will not dim. It will either turn on or off. It will turn on, only when power reaches 100% in the code. Anything below that the light will be off.
I have confirmed that if I connect a LED directly to the Arduino, it will fade in and out properly.
int led = 9; // the pin that the LED is attached to
int brightnesslow = 0; // low power setting of the LED
int brightnesshigh = 255; // high power setting of the LED
int fadeAmount = 20; // how many points to fade the LED by
int power = 0;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.println(F("Initialize System - Beacon Slow Pulse"));
analogWrite(led,255);
}
void loop() {
for(power=brightnesslow;power<=brightnesshigh;power+=fadeAmount){
//analogWrite(led,brightnesslow);
analogWrite(led,power);
//brightnesslow = brightnesslow - fadeAmount;
Serial.print("lampValue -> ");
int powerLevel = power*100/brightnesshigh;
Serial.print(powerLevel);
Serial.println("A:%");
delay(30);}
for(power=255;power<=255;power+=fadeAmount){
//analogWrite(led,brightnesslow);
analogWrite(led,power);
//brightnesslow = brightnesslow - fadeAmount;
Serial.print("lampValue -> ");
int powerLevel = power*100/brightnesshigh;
Serial.print(powerLevel);
Serial.println("B:%");
delay(800);}
for(power=brightnesshigh;power>=brightnesslow;power-=fadeAmount){
//analogWrite(led,brightnesslow);
analogWrite(led,power);
//brightnesslow = brightnesslow - fadeAmount;
Serial.print("lampValue -> ");
int powerLevel = power*100/brightnesshigh;
Serial.print(powerLevel);
Serial.println("C:%");
delay(30);}
for(power=15;power>=10;power-=fadeAmount){
//analogWrite(led,brightnesslow);
analogWrite(led,power);
//brightnesslow = brightnesslow - fadeAmount;
Serial.print("lampValue -> ");
int powerLevel = power*100/brightnesshigh;
Serial.print(powerLevel);
Serial.println("C:%");
delay(800);}
}
I that case, the issue is not with your code. Indeed, I can run your code with an led on a breadboard and see it pulse bright and dim.
Ae you certain that your bulb is an incandescent bulb and not some led bulb?
If the bulb is correct, then the issues must be with the Krida dimmer or how you have it connected. Can you provide a hand drawing of the connections. A photo would also be helpful.
Best, would be if you had an oscilloscope to put on the Krida output to see what is going on.
Thank you for your prompt reply. On the Krida dimmer, I can see the power status light dim in and out in sync with the code. once the code is at 100% power, the 50Hz light on the Krida will come on and so will the incandescent bulb (not a LED).
Arduino Pin # 9 connected to VCC on Krida Dimmer
Arduino 5V connected to PWM on Krida Dimmer
Arduino GND connected to GND on Krida Dimmer
Hi cattledog, I came across this post looking for info on coding a similar project using the same PWM module, but instead of lighting, I'm looking to control a 120vAC/60hz asynchronous pond pump. Would like to have 10 steps from 100% (Full flow) to 25% flow. Coding something like this is new to me, so ANY help in editing this code or links to some documentation would be appreciated, Thanks !
Not all ac motors are designed to work at variable speeds based on ac phase cutting. What documentation to you have on the motor? I'm not very knowledgeable about motors, and you may want to post a new post in the Motors Mechanics and Power section of the forum.
If the pump motor is indeed variablespeed with ac phase cutting, then you will be using analogWrite() with 10 different duty cycles to set the pump speeds. You will need to do some experimentation to determine the level to use for each flow rate. I doubt the relationship will be linear and you may not have much range between full speed and some low end cut off.
Hey thanks for the response, I actually have the pump running off a variable speed controller that the pump plugs into and I believe works the same way. If my current device had the ability to lower/increase speed via schedule or timer I would continue to use it .. but it does not. The device can turn the pump on/off or lower/increase the flow via a 2.4ghz remote, the unit itself or via an app (but only on the same wifi network). Id like to lower the pump speed (or turn off) with schedules or remotely if needed. This will also be part of a larger multi relay project to turn on lights, smaller pumps, air bubblers & measure the water temp.
I'm not sure what the number range is for 0-100 % power is just 0-100 ? If so, then looking at the code the previous poster used (below), would I create this for each 'increase step', where brightnesslow (or in my case, pump speed ) would be a 0 [off] brightnesshigh =100 [full on] with the fadeamount (in my case speed-step) being 7.5 as the step increment to get from 25 to 100 ?