Seg 4: Experiment and code

4.4 Experiment and code

This experimental schematic is very simple, and similar to the example one. Note that, the port connected with LED must be one of the ports 3,5,6,9,10,11 on Arduino board. Certainly, if you use other method, it is maybe different. Its schematic and circuits can be seen in Figure 4-4, 4-5.

Figure 4-4 Experimental schematic

Figure 4-5 Experimental circuit

Code solution

01 / Program 4: Arduino and PWM for continuous variations from bright to dim for LED
02 int brightness = 0;    // define the int variation brightness, which is used to change the brightness of LED.
03 int fadeAmount = 5;    //define variation fadeAmount used as a increasement or decreasement.
05 void setup()  { 
07   pinMode(9, OUTPUT);// set port 9 as OUTPUT
08 } 
10 void loop()  { 
12   analogWrite(9, brightness);//write brightness to port 9
14   brightness = brightness + fadeAmount;//change brightness used in the next cycle
16   if (brightness == 0 || brightness == 255{
17     fadeAmount = -fadeAmount ; //transform in high level (5V) and low level (0V)
18   }     
20   delay(50); //delay 50ms, make the value brightness+fadeAmount lasts 50ms
21 }

4.5 Key points to summarize

1) Although digital signal is superior than analog signal. But in some applications, it is a must to use analog signal, like motor, music, and so on.

2) The scope of parameter “value” in the Arduino sentence analogWrite(pin,value) is 0~255. We should understand the relationships between duty cycle and the output voltage.

3) There are two kinds of ports on the Arduino board, in which, only some ports can be used to output the analog signal.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>