Can I turn on a dimmerable light bulb with sound ?

Hi guys, I am a design student from Italy and I am really new with Arduino coding but I am trying with all my effort to reach a project. My knowledge in electronic is improving but I am still at the beginning.

I have an IKEA lamp (no battery, only plug) and I want to give three different light effects that started with the sound (input): with low volume I will have 1 fading leds effect, with medium volume I want to dimmer on a light bulb and with high volume I want to start a blinking light effect.

First Question: is it possible to dimmer on a light bulb with the sound? I saw many tutorial where it's possible to turn on and off a bulb with a relay module clapping hands. What if I make a sound and this allows me to turn on the light but gradually (from 0 to maximum of luminosity) ? Is it possible to do ?
As well I want to share my circuit till now (I need to change the potentiometer with the sound sensor and program it as the input) and than link the two light effects on one sketch, so If I talk loud I'll have one light effect, if I talk low I will have another light effect.

Second question: how can I link two different tasks, activated by the same input (sound) so they can work independently ?

Thank you very much.

Eli

You should be able to drive the dimmer unit using PWM, so rather than using the potentiometer, connect it to a PWM pin and use analogWrite() to set the level between 0-255

Deva_Rishi:
You should be able to drive the dimmer unit using PWM, so rather than using the potentiometer, connect it to a PWM pin and use analogWrite() to set the level between 0-255

No, you don't want to use PWM for AC control. Use zero crossing detection and cycle chopping. For an example, see: Arduino Playground - ACPhaseControl

till now (I need to change the potentiometer with the sound sensor and program it as the input) and than link the two light effects on one sketch,

Is the potentiometer working?

You should be able to drive the dimmer unit using PWM,

That's not clear with the information given, since the dimming module also has a zero-crossing output.

colaeli,
Do you have a link to the specs for the dimmer module? It looks a little strange because it has "PWM" and Zero-Crossing" inputs. If it actually works with PWM that's easier than a regular [u]phase-control dimmer[/u]. (It would be probably be using phase-control "internally" even if can be controlled with PWM because that's how AC dimmers work.)

Do you have a link to the microphone module you're using? There are 3 or 4 kinds of microphone modules. Some put-out a digital pulse when the sound crosses a preset loudness, some put-out a varying DC voltage proportional to the loudness, and most put-out an amplified & biased audio signal (biased because the Arduino can't read the negative half of the AC audio waveform). And, some have a multiple-different outputs. Some put-out I2C digital audio.


Of course, if you have a microphone module that can detect the sound, and if you can dim the lamp, you can write software to "connect" the input (sound signal) and output (lamp). I've made several sound-activated lighting effects, some AC and some DC (but I've never used dimming and most of my original code was lost in a hard drive crash).

Use the serial monitor (like in the Analog Read Serial Example) to "see" what kind of numbers you're getting from the microphone board. Then it's mostly a matter of if-statements to control the lamp.

DVDdoug:
Do you have a link to the specs for the dimmer module? It looks a little strange because it has "PWM" and Zero-Crossing" inputs.

I have several of those modules. While that input is called "PWM", it's actually just the Triac's gate control. You have to use phase chopping as I posted in Reply #3.

THIS IS THE DIMMER MODULE

https://www.amazon.co.uk/ILS-RobotDyn-Dimmer-Control-Channel/dp/B07911Y37L

THIS IS THE MICROPHONE MODULE

Here I have attached the Arduino code made from Robojax (thank you) with the potentiometer.
I am trying to convert the input potentiometer with the microphone but first I have to understand how the mic sensor works.

dimmerlight.ino (1.64 KB)

No, you don't want to use PWM for AC control. Use zero crossing detection and cycle chopping.

Clearly the unit shows that it takes a PWM input. There are definitely good ways to use zero-crossing detection and cycle-chopping, using an Arduino, but they are quite CPU intensive. If you have a unit that takes PWM as an input, you should use that and keep your processing time for other things. Let your unit, that has it's own CPU (probably a PIC of some kind) take care of zero-crossing detection and cycle-chopping for you. I am actually considering using an ATtiny to do the dimming and use an analog input myself, since the memory requirements are not so big. Though if you want to dim multiple lights (at different levels) then something bigger would be required.

Deva_Rishi:
Clearly the unit shows that it takes a PWM input.

See Reply #5. I own several of those units, have the schematic, and know what I'm talking about. Do you?

gfvalvo:
See Reply #5. I own several of those units, have the schematic, and know what I'm talking about. Do you?

So it works with the pot meter attached, well then obviously that potmeter can do things that most potmeters can't !! If the specs say it takes PWM then i guess you have different units. Mind you i do agree some part seems to be missing on the board, cause all i see is the Triac and the Optocoupler, so then something is not correct in the OP's statement. There are actually a few more parts missing, being the filters ! using 1 unit like that should not get you into to much trouble but running a few next to each other will generate enough noise to cause them to malfunction.

Deva_Rishi:
So it works with the pot meter attached, well then obviously that potmeter can do things that most potmeters can't !!

Try reading the code that OP attached in Reply #7. It reads the "potmeter" using analogRead() to get a number. That number is passed to a function in the RDBdimmer Library. That library takes the number and uses interrupts and hardware timers to implement AC phase chopping.

If the specs say it takes PWM....

They don't. The mislabeled silkscreen on the board does.

but running a few next to each other will generate enough noise to cause them to malfunction.

The manufacture makes units with up to Four Such Circuits Ganged on a Single Board. I haven't tried that one yet. But, the Single Ciruit Unit does work --- assuming you implement the required phase chopping with the processor that you connect. They won't work satisfactorily for AC control if you just drive the (mislabeled) "PWM" pin with an Arduino PWM output.

Try reading the code that OP attached in Reply #7. It reads the "potmeter" using analogRead() to get a number

I was looking at the schematic in the Original Post.

The manufacture makes units with up to Four Such Circuits Ganged on a Single Board. I haven't tried that one yet

I saw a similar one for a double but that already includes a capacitor per unit.

Guys,

I am asking you to help me with the coding, cause I don't know how to link 2 light effects.

1 light effect: leds with random fading
2 light effect: dimmer light bulb

I want that my input is the volume: microphone as sensor to give me an input. It's a microphone with 4 pins (vcc, ground, one pin to A0, another pin to D7).

I want two lights effects, one it will start with one volume treshold (it's related to the leds on the circuit and it's called rainlight) (here there is only the code of the leds effect)

int LED1 = 6;
int LED2 = 5;
int LED3 = 11;
int LED4 =3;
int LED5 = 9;
int LED6 = 10;
int brightness1 = 0;
int fadeAmount1 = 5;
int fadeAmount2 = 5;
int fadeAmount3 = 5;
int brightness2 = 50;
int brightness3 = 100;
int buttonPoll = 0;

void setup() {
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
}


void loop() {
 
 {analogWrite(LED1, brightness1);              
  analogWrite(LED3, brightness2);       
  analogWrite(LED2, brightness2);                     
  analogWrite(LED5, brightness1);     
  analogWrite(LED4, brightness3); 
  analogWrite(LED6, brightness3);
  brightness1 = brightness1 + fadeAmount1;
  brightness2 = brightness2 + fadeAmount2;
  brightness3 = brightness3 + fadeAmount3;
    delay(5);//time between updating and writing the brightness variable
     } 
 if (brightness1 <= 0 ||brightness1 >= 255) {
  fadeAmount1 = -fadeAmount1;
 }else if (brightness2 <= 0 ||brightness2 >= 255){
  fadeAmount2 = -fadeAmount2;
 }else if (brightness3 <= 0 ||brightness3 >= 255) {
  fadeAmount3 = -fadeAmount3;
    }
   
    delay(5);
  }

and another one will give me the dimmerable light. I have a code when a dimmerable light works with a potentiometer as input. I want to change this putting a microphone as input. So when i talk with a medium volume it will turn on just once my light (gradually), and after 10 seconds it will turn off by herself.
Here there is the code with the potentiometer made by Robojax

#include <RBDdimmer.h>//

//#define USE_SERIAL  SerialUSB //Serial for boards whith USB serial port
#define USE_SERIAL  Serial
#define outputPin  12 
#define zerocross  2 // for boards with CHANGEBLE input pins
#define potPin A0 // define the input pin for potentiometer

/// fix potentiometer issue
const int potMin = 70;// watch video (link above) for details
const int potMax = 804;// watch video (link above) for details
int potValue;// watch video (link above) for details

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int outVal = 0;

void setup() {
  USE_SERIAL.begin(9600);
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE) 
}

void loop() 
{
  potValue =analogRead(potPin);// read potentiometer

 correctValue(); 
  // watch video (link above) for details
  outVal = map(potValue, 22, 1023, 100, 0); // analogRead(analog_pin), min_analog, max_analog, 100%, 0%);
  USE_SERIAL.print("potValue:"); 
  USE_SERIAL.print(potValue);
  USE_SERIAL.print(" ");     
  USE_SERIAL.print(outVal); 
  USE_SERIAL.println("%");   
  dimmer.setPower(outVal); // name.setPower(0%-100%)
}

/*
 * correctValue()
 * Corrects the value for AC dimmer module
 * Written by Ahmad Shamshiri on Oct 25, 2019
 * watch my Robojax YouTube video vor details
 * www.Robojax.com
 */
void correctValue()
{
   if(potValue <potMin)
  {
    potValue =potMin;
  }
  if(potValue >potMax)
  {
    potValue =potMax;
  }   
}//correctValue() end

In the attached image I have copied my attempt to merge the sketches together but I think that the red parts are connected with the potentiometer so I think i might eliminate them.
As I told you I am really new with C++.
Thank you a lot.

You need to divide and conquer. I’d start with the code that uses the RobotDyn Phase Chopper Module.

Note how it simply does an analog read to get the value from the potentiometer (configured as a voltage divider) and scales it to the appropriate range. It then calls the setPower() function from the RBDdimmer library. This library uses interrupts and hardware timers in the processor to implement the phase chopping (not PWM) using the module. You can substitute properly-scaled readings from your microphone in place of the potentiometer.

Adding in the LED code will require structural changes to make it non-blocking. As is, the delay() statements will make in non-responsive.

Hi guys,

I am new with the programming language C++, I am an italian designer and I have a project that I need to send before monday 13th.
IN this project I want to create two light effects for a lamp. I have the codes for those two light effects but I want to use the mic sensor as a switch: with one volume treshold I will have a light effect with fading leds that will stay in loop for 10 seconds, and with another treshold (with loud volume) it will start the second light effect that is a light bulb that blinks on and off that will stay in loop for another 10 seconds.
I am using those sensors :

https://robotdyn.com/ac-light-dimmer-module-1-channel-3-3v-5v-logic-ac-50-60hz-220v-110v.html

How can I program this ?

I provided some advice on how to proceed in your previous thread. Why have you started another one?

I've merged your other cross-post @colaeli.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.