Arduino Tutorial Series : Lesson 05 - Analog Output

Upto now you have mastered digital inputs and output stuff. But, How about analog?

analogWrite() function can be used to output analog voltages.

Syntax :

analogWrite( OUTPUT_PIN, ANALOG_LEVEL);

ANALOG_LEVEL input parameter gets an integer number 0 -255 representing voltages 0- 5v.
Let's look onto some examples of analog output.

LED Fading

Hardware


Hardware setup is same as the Blinky example.

Arduino Sketch

Download the code ledFade.ino

for (int i=0; i<256; i++)
{
analogWrite(LED, i);
delay(10);

}

In this example we use a for loop to fade out and fade in the LED. The Variable i sets the output analog voltage value.


Small Motor Speed Control

WARNING : Use this method for smaller motors only, using larger motors can cause permanent damage to the Arduino due to high current draw.

Hardware

What you need :

  • Small motor
  • PN2222 transistor
  • 2 AA baterries


Arduino Code

Download the code motorSpeed.ino

The code isn't much different to above one. It does the same thing.

Note: 

Arduino can not output ' actual ' analog voltages. But digital circuits use a nice trick to emulate analog output. This is called PWM (Pulse Width Modulation).

Image result for pwm

PWM signals send pulses of 5v signals. The effective voltage output depends on the Duty Cycle of PWM signal.

No comments:

Powered by Blogger.