The input of the circuit is from a doorbell transformer and provides 21V AC. Then the AC input passes through a bridge rectifier to produce a DC output (of about 26V). The output from the bridge rectifier is smoothed with a capacitor, and then a voltage rectifier converts. Bridgold 20pcs KBP307 Flat Bridge Bridge Rectifier,3A/700V Single Phase,4-Pin $7.99 Ltvystore 2W10 DIP-4 2A 1000V Bridge Diode Rectifier for Arduino, Pack of 30.
This article is another step forward in learning more about Arduino. Â In our previous article, I have written in detail about blinking an LED using Arduino. We have demonstrated 5 simple led based projects using arduino, which will help you to learn its basic concepts.
1. Blinking Two LED’s using arduino
2. Control LED using a Push button switch
3. Toggle an LED using Push button switch
4. Toggle 2 LED’s using a Push button switch
1. Blinking Two LED’s using Arduino
As a beginner, if you have tried the “Hello World” program to blink an LED using Arduino; you can try to blink Two LED’s as next project. There are 14 I/O (input/output) pins in your Arduino uno board. These pins are numbered from 0 to 13. They can be configured as either input or output in the sketch you create for arduino. If you have learned the “Hello World” program carefully, you now know that input/output configuration of pins has to be done inside the setup() function. So here is the circuit diagram to blink 2 led’s using arduino.
The kybalion book pdf. Sketch to Blink Two LED’s using Arduino
The only difference in this sketch is use of 2 pins in output mode. I have used pin number 12 and 13 as output. I have configured them as output inside the setup() function. Inside the loop(), I have written commands to blink LED’s alternatively. When LED1 is ON, LED2 will be OFF. After 1 second LED1 will turn OFF and at the same time LED2 will turn ON. Wait another 1 second and you will see LED2 turning OFF and LED1 turning ON. This cycle repeats.
I have added a photograph of the practical setup I made below.
You can watch video of the same circuit below.
2.Control LED with Push Button
If you observe carefully, so far we were just playing with some outputs. We made one LED blink and then we stepped ahead to make two LED’s blink. In both cases we wrote software commands to make our arduino blink led’s automatically at an interval of 1 second. What if we want to control led’s ON and OFF time based on a user input? Â This means, I want to give an input manually and based on my input LED should turn ON and OFF. We can use a push button switch to give user input to arduino. In fact, we can use any type of a simple switch like Push to On or Push to Off or a mini push button switch. In this example I am using a “normally open” mini push button switch. A normally open push button switch will be in its open state by default. This switch will close for the time we keep its actuator pressed. If you want to know more about working of different push button switches, you can read our detailed article on push button switches.
I have added the circuit diagram to control LED with arduino using a push button switch. Â To connect push button to arduino, we need one of the digital I/O pins configured as a digital input. In this example, I have set pin number 7 as a digital input. So we should connect the push button switch to pin 7 of arduino as shown in circuit. A reference voltage should be connected to one end of switch and the other end of switch should be connected to ground. To avoid a short circuit between pin number 7 and ground, you should connect a resistor (preferable a 10K ohm) in between. The reference voltage is used to detect ON state or closed state of the push button. Arduino board has a readily available +5 volts reference on power pins cluster. When the push button is pressed, the reference voltage line will get connected to pin number 7. Â This voltage will drop across the 10K ohm resistor. So when push button is pressed, a +5 volts is available at pin 7 and this will be considered as state HIGH. On the other hand, when the push button is released (residing in its normally open state), there is no reference voltage line connected at pin 7. On this state, the voltage across 10K resistor is 0 volts (ground potential). This same potential is at pin 7 as well and will be considered as state LOW. This is how ardunio distinguishes between closed (ON) and open (OFF) states of push button switch.
Lets get into the program side of controlling LED using push button switch. In this program, the highlight is instruction to read push button state. APL (Arduino programming language) has an instruction named digitalRead() – which reads a digital input given at the configured input pin. In our program, this instruction reads the status at pin 7 and returns a value according to what it has read. In our example this instruction reads voltage level at pin number 7; returns HIGH if its +5 volts and returns LOW if its 0 volts. Since it returns a value, we have to assign this instruction to a variable while we write the program. We have used the variable val to store the value returned by the instruction digitalRead(). The push button switch is connected to pin 7 and we have assigned this pin 7 to a variable named SW inside our sketch. Inside the setup() function, we have configured this pin 7 (the SW variable) as input using pinMode() instruction. So here is the program.
3.Toggle LED using Pushbutton
Lets get into next project which is even more interesting. Here we are going to toggle an LED using a push button switch. Toggle means to change state. Our objective here is turn LED ON with first push button press and turn LED OFF with next push button press. This cycle of ON and OFF should continue with each push button press. Â The same circuit diagram given above is enough to do this project as we are not manipulating any hardware connection. We just need to change our software (sketch) to change the behavior of this circuit.
Here is the sketch to toggle an LED using push button switch.
I used a “normally open” mini push button switch to implement the circuit. This means a push button press always gives us a “HIGH” state. In other words, we have to sense the closed state of push button switch to turn LED ON and OFF. Turning LED ON and turning LED OFF Â both depends on a single event – the press on actuator of push button switch (its closed state – when the voltage at switch = HIGH). We can do this program in many ways. An efficient program always will have less lines of code. In this program, I used boolean instructions and a complement operator. In the program a variable named state is declared as boolean and I initialized it as true. You may read more about boolean instruction to get deep idea. A boolean instruction has only two possible values, either true or false. The next highlight of the program is to use of complement operator ( ! ). This is the same negation operator we see in 8051 and other micro controllers. For example, we have an instruction called CPL in 8051 instruction set. This instruction compliments the values in accumulator (0’s with 1 and 1’s with 0’s). In digital electronics, a NOT gate performs the same task.
So here is the working of program. We initialized variable state as true. Â Other lines of code are the same we used in previous programs. You already know what is written inside setup(). Lets come to loop(). We sense input of switch with digitalRead(SW) and store it in an integer variable val. Now we check for the push button press by continuously checking if the variable val has ever registered a HIGH. If it ever registers a high, we compliment the status of state variable and save it to the same variable. Now if the state variable was TRUE before, it has been complimented to FALSE. We write the status of state variable to LED. Based on the value of state variable LED will turn ON and OFF. LED will turn ON if state variable holds a TRUE and LED will turn OFF if state variable holds a FALSE. This process of reading the push button switch and complimenting the state variable continuous.
Note:- In our program, the first push button press actually turns the LED OFF. It will turn ON only with second push button press. From then it will alternate between ON and OFF with each push button press. If you want it to turn ON with first push button press, you just need to make a change in the boolean declaration statement. Declare the variable as FALSE initially.
Note2:- You might have noted a delay(120); instruction just below the val=digitalRead(SW); instruction. It is called software debouncing technique. This a practical aspect of the circuit. If you are to write the program based on theory, you don’t need a delay instruction here. But there is a practical problem. You may upload the code with out this delay instruction and see the behavior of circuit. Some push button press will actually toggle and some other will not yield an expected result. This behavior is due to 2 reasons. 1) The push button is a mechanical switch. One push button press will yield a series of high pulses (bcz of the vibration created when 2 mechanical parts get in contact) in practice. 2) Arduino is a really fast prototyping platform. In fact its not arduino, its the micro controller used in the board that’s really fast. In an arduino uno, Atmega328 is used which is of 20 MIPS execution capability. This means the controller can execute 20 million instructions per second. It is very very fast than we can imagine. So arduino will sense all these series of high pulses created by one push button press. But we dont need arduino to sense all these pulses. We just need 1 HIGH pulse per push button press. This is a problem created by the bouncing switch and we eliminate this problem through a debouncing technique. There are 2 types of debouncing techniques. Here we apply software debouncing. You may read more about debouncing techniques in our article.
4. Toggle 2 LED using Pushbutton
Our next project is to toggle 2 LED’s using a single push button switch. Here we need one more LED and little tweak in the software. I have added the circuit diagram and program below. Â I respect your intelligence. Windows 9 iso free download windows 7. You don’t need an explanation for this circuit and program after learning this much.
I have added the sketch below. Read the sketch carefully. We just need to add a few lines to above program. Its really that simple.
I have added a video of the practically implemented circuit. You can watch it below.
Note 3:- I already wrote that there are different ways to create a program. This same program for toggling LED can be written without using a boolean variable. If we dont use a boolean variable, we can not make use of the compliment operator. This simply means we have to handle the task of switching LED states inside our code. I have added a sketch to toggle an LED without using boolean variable. If you take a closer look, you can see this program is very big and uses more variables and instructions than our previous program (with boolean variable). But both sketches leads the same desired output. So which program is more efficient ? The one with less lines of code!
I will explain the code later. Ask in comments if you have any doubts.
WAIT!! before you decide to build this, it is good to know that a similar dimmer is available at Aliexpress at cost that is hard to beat (currently 2.70 euro)
WARNING: Some people try to build this with an optocoupler with zerocrossing coz 'that is better' right? Some are even told in electronics shops it is better to use such an optocoupler. WRONG. This will only work with a random fire optocoupler: NOT igniting at zerocrossing is the principle of this dimmer.
Switching an AC load with an Arduino is rather simpel: either a mechanical relay or a solid state relay with an optically isolated Triac. (I say Arduino, but if you use an 8051 or PIC16F877A microcontroller, there is stuff for you too here.)
It becomes a bit more tricky if one wants to dim a mains AC lamp with an arduino: just limiting the current through e.g. a transistor is not really possible due to the large power the transistor then will need to dissipate, resulting in much heat and it is also not efficient from an energy use point of view.
Phase cutting
One way of doing it is through phase control with a Triac: the Triac then is fully opened, but only during a part of the sinus AC wave. This is called leading edge cutting.
One could let an Arduino just open the Triac for a number of microseconds, but that has the problem that it is unpredictable during what part of the sinus wave the triac opens and therefore the dimming level is unpredictable. One needs a reference point in the sinus wave.
For that a zero crossing detector is necessary. This is a circuit that tells the Arduino (or another micro controller) when the sinus-wave goes through zero and therefore gives a defined point on that sinus wave.
Opening the Triac after a number of microseconds delay starting from the zero crossing therefore gives a predictable level of dimming.
Pulse Skip Modulation
Another way of doing this is by Pulse Skip Modulation. With PSM, one or more full cycles (sinuswaves) are transferred to the load and then one or more cycles are not. Though effective, it is not a good way to dim lights as there is a chance for flickering. Though it might be tempting, in PSM one should always allow a full sinuswave to be passed to the load, not a half sinus as in that case the load will be fed factually from DC which is not a good thing for most AC loads. The difference between leading edge cutting and PSM is mainly in the software: in both cases one will need a circuit that detects the zero crossing and that can control a triac.
A circuit that can do this is easy to build: The zero crossing is directly derived from the rectified mains AC lines – via an optocoupler of course- and gives a signal every time the wave goes through zero. Because the sine wave first goes through double phased rectification, the zero-crossing signal is given regardless whether the sinus wave goes up through zero or down through zero. This signal then can be used to trigger an interrupt in the Arduino.
Phase cutting
One way of doing it is through phase control with a Triac: the Triac then is fully opened, but only during a part of the sinus AC wave. This is called leading edge cutting.
One could let an Arduino just open the Triac for a number of microseconds, but that has the problem that it is unpredictable during what part of the sinus wave the triac opens and therefore the dimming level is unpredictable. One needs a reference point in the sinus wave.
For that a zero crossing detector is necessary. This is a circuit that tells the Arduino (or another micro controller) when the sinus-wave goes through zero and therefore gives a defined point on that sinus wave.
Opening the Triac after a number of microseconds delay starting from the zero crossing therefore gives a predictable level of dimming.
Pulse Skip Modulation
Another way of doing this is by Pulse Skip Modulation. With PSM, one or more full cycles (sinuswaves) are transferred to the load and then one or more cycles are not. Though effective, it is not a good way to dim lights as there is a chance for flickering. Though it might be tempting, in PSM one should always allow a full sinuswave to be passed to the load, not a half sinus as in that case the load will be fed factually from DC which is not a good thing for most AC loads. The difference between leading edge cutting and PSM is mainly in the software: in both cases one will need a circuit that detects the zero crossing and that can control a triac.
A circuit that can do this is easy to build: The zero crossing is directly derived from the rectified mains AC lines – via an optocoupler of course- and gives a signal every time the wave goes through zero. Because the sine wave first goes through double phased rectification, the zero-crossing signal is given regardless whether the sinus wave goes up through zero or down through zero. This signal then can be used to trigger an interrupt in the Arduino.
PWM dimming
PWM dimming, as in LEDs is not done frequently with AC loads for a number of reasons. It is possible though. Check this instructable to see how.
It goes without saying that there needs to be a galvanic separation between the Arduino side of things and anything connected to the mains. For those who do not understand 'galvanic separation' it means 'no metal connections' thus ---> opto-couplers. BUT, if you do not understand 'galvanic separation', maybe you should not build this.
The circuit pictured here does just that. The mains 220Volt voltage is led through two 30k resistors to a bridge rectifier that gives a double phased rectified signal to a 4N25 opto-coupler. The LED in this opto-coupler thus goes low with a frequency of 100Hz and the signal on the collector is going high with a frequency of 100Hz, in line with the sinusoid wave on the mains net. The signal of the 4N25 is fed to an interrupt pin in the Arduino (or other microprocessor). The interrupt routine feeds a signal of a specific length to one of the I/O pins. The I/O pin signal goes back to our circuit and opens the LED and a MOC3021, that triggers the Opto-Thyristor briefly. The LED in series with the MOC3021 indicates if there is any current going through the MOC3021. Mind you though that in dimming operation that light will not be very visible because it is very short lasting. Should you chose to use the triac switch for continuous use, the LED will light up clearly.
Mind you that only regular incandescent lamps are truly suitable for dimming. It will work with a halogen lamp as well, but it will shorten the life span of the halogen lamp. It will not work with any cfl lamps, unless they are specifically stated to be suited for a dimmer. The same goes for LED lamps
NOTE! It is possible that depending on the LED that is used, the steering signal just does not cut it and you may end up with a lamp that just flickers rather than being smoothly regulated. Replacing the LED with a wire bridge will cure that. The LED is not really necessary. increase the 220 ohm resistor to 470 then
STOP: This circuit is attached to a 110-220 Voltage. Do not build this if you are not confident about what you are doing. Unplug it before coming even close to the PCB. The cooling plate of the Triac is attached to the mains. Do not touch it while in operation. Put it in a proper enclosure/container.
WAIT: Let me just add a stronger warning here: This circuit is safe if it is built and implemented only by people who know what they are doing. If you have no clue or if you are doubting about what you do, chances are you are going to be DEAD!DO NOT TOUCH WHEN IT IS CONNECTED TO THE GRID
Materials
Zerocrossing
4N25 €0.25 or H11AA1 or IL250, IL251, IL252, LTV814 (see text in the next step)
Resistor 10k €0.10
bridge rectifier 400 Volt €0.30
2x 30 k resistor 1/2 Watt (resistors will probably dissipate 400mW max each €0.30
1 connector €0.20
5.1 Volt zenerdiode (optional)
Lamp driver
LED (Note: you can replace the LED with a wire bridge as the LED may sometimes cause the lamp to flicker rather than to regulate smoothly)
MOC3021 If you chose another type, make sure it has NO zero-crossing detection, I can't stress this enough DO NOT use e.g. a MOC3042
Resistor 220 Ohm €0.10 (I actually used a 330 Ohm and that worked fine)
Resistor 470 Ohm-1k (I ended up using a 560 Ohm and that worked well)
TRIAC TIC206 €1.20 or BR136 €0.50
1 connector €0.20
Other
Piece of PCB 6x3cm
electric wiring
That is about €3 in parts
PWM dimming, as in LEDs is not done frequently with AC loads for a number of reasons. It is possible though. Check this instructable to see how.
It goes without saying that there needs to be a galvanic separation between the Arduino side of things and anything connected to the mains. For those who do not understand 'galvanic separation' it means 'no metal connections' thus ---> opto-couplers. BUT, if you do not understand 'galvanic separation', maybe you should not build this.
The circuit pictured here does just that. The mains 220Volt voltage is led through two 30k resistors to a bridge rectifier that gives a double phased rectified signal to a 4N25 opto-coupler. The LED in this opto-coupler thus goes low with a frequency of 100Hz and the signal on the collector is going high with a frequency of 100Hz, in line with the sinusoid wave on the mains net. The signal of the 4N25 is fed to an interrupt pin in the Arduino (or other microprocessor). The interrupt routine feeds a signal of a specific length to one of the I/O pins. The I/O pin signal goes back to our circuit and opens the LED and a MOC3021, that triggers the Opto-Thyristor briefly. The LED in series with the MOC3021 indicates if there is any current going through the MOC3021. Mind you though that in dimming operation that light will not be very visible because it is very short lasting. Should you chose to use the triac switch for continuous use, the LED will light up clearly.
Mind you that only regular incandescent lamps are truly suitable for dimming. It will work with a halogen lamp as well, but it will shorten the life span of the halogen lamp. It will not work with any cfl lamps, unless they are specifically stated to be suited for a dimmer. The same goes for LED lamps
NOTE! It is possible that depending on the LED that is used, the steering signal just does not cut it and you may end up with a lamp that just flickers rather than being smoothly regulated. Replacing the LED with a wire bridge will cure that. The LED is not really necessary. increase the 220 ohm resistor to 470 then
STOP: This circuit is attached to a 110-220 Voltage. Do not build this if you are not confident about what you are doing. Unplug it before coming even close to the PCB. The cooling plate of the Triac is attached to the mains. Do not touch it while in operation. Put it in a proper enclosure/container.
WAIT: Let me just add a stronger warning here: This circuit is safe if it is built and implemented only by people who know what they are doing. If you have no clue or if you are doubting about what you do, chances are you are going to be DEAD!DO NOT TOUCH WHEN IT IS CONNECTED TO THE GRID
Materials
Zerocrossing
4N25 €0.25 or H11AA1 or IL250, IL251, IL252, LTV814 (see text in the next step)
Resistor 10k €0.10
bridge rectifier 400 Volt €0.30
2x 30 k resistor 1/2 Watt (resistors will probably dissipate 400mW max each €0.30
1 connector €0.20
5.1 Volt zenerdiode (optional)
Lamp driver
LED (Note: you can replace the LED with a wire bridge as the LED may sometimes cause the lamp to flicker rather than to regulate smoothly)
MOC3021 If you chose another type, make sure it has NO zero-crossing detection, I can't stress this enough DO NOT use e.g. a MOC3042
Resistor 220 Ohm €0.10 (I actually used a 330 Ohm and that worked fine)
Resistor 470 Ohm-1k (I ended up using a 560 Ohm and that worked well)
TRIAC TIC206 €1.20 or BR136 €0.50
1 connector €0.20
Other
Piece of PCB 6x3cm
electric wiring
That is about €3 in parts