«

»

How To Brighten And Dim LED ?

Hardware setup

وصل زي الصورة دي بالظبط

Connect an LED to digital pin 11 on the Arduino hardware through a 1KOhm resistor.

canflag_anim3

Create an arduino object

افتح الماتلاب و اكتب الكلام باللون الازرق

a = arduino();                                                                         

       a ده معناه ان اننا موصل اردوينو واسمه     

If you have more than one Arduino board connected, specify the port and board type.

a = arduino('com23', 'uno');

Turn LED on and off

Write value 1 or true to digital pin 13 turns on the built-in LED and write a value of 0 or false turns it off. Execute the following command at the MATLAB prompt to turn the LED off and on.

هنا هنبتدي نشغل و نطفي اللمبة

writeDigitalPin(a, ‘D13’, 0);                                       

    pin3عشان نطفي اللمبة هنبعت0 علي

pause(2);

writeDigitalPin(a, ‘D13’, 1);                               

             pin3عشان نشغل اللمبة هنبعت 1علي

Brighten and dim LED

ازاي تنور لمبة و تتحكم في شدة الاضاء

قبل الكود لازم تفهم الاتي

A duty cycle is the percentage of one period in which a signal is active. A period is the time it takes for a signal to complete an on-and-off cycle. As a formula, a duty cycle may be expressed as:

D = \frac{T}{P} \times 100\%

where D is the duty cycle, T is the time the signal is active, and P is the total period of the signal.

دلوقتي ممكن نبتدي كتابة الكود

Send pulse signals of specified width to the PWM pins on the Arduino hardware. PWM signals can light up LEDs connected to the pin. The duty cycle of the pulse controls the brightness of the LED. Calculate the amount that the LED brightens and dims by dividing the max and min duty cycle for the pin by the number of iterations.

brightness_step = (1-0)/20;

for i = 1:20

     writePWMDutyCycle(a, ‘D11’, i*brightness_step);    يعني   i*brightness_step =duty cycle =i*1/20

     pause(0.1);

end

for i = 1:20

     writePWMDutyCycle(a, ‘D11’, 1-i*brightness_step);

     pause(0.1);

end

You can also brighten and dim the lights by changing the voltage of the PWM signal. Calculate the amount that the LED brightens and dims by dividing the max and min voltage for the pin by the number of iterations.

brightness_step = (5-0)/20;

for i = 1:20

       writePWMVoltage(a, ‘D11’, i*brightness_step);

        pause(0.1);

end

 for i = 1:20

        writePWMVoltage(a, ‘D11’, 5-i*brightness_step);

         pause(0.1);

end

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=""> <s> <strike> <strong>