Analog signals has level/amplitude associate with it. One way to understand the difference between analog and digital signals is a light switch. A standard on/off switch would be an example of how a digital signal works. In contrast, dimmer switch adjusts the level of the light’s brightness. A dimmer is analog.
The analog output is used to control the level of energy put onto a pin. In contrast, a pin that is configured to be Digital can only be on (fully on) or off, which is 1 and 0 in coding. To set an LED to be half brightness, for example, we can use the AWrite() function and set the output to 50, which is 50%.
AWrite(4, 50) # Set pin 4 to analog at 50% energy
Jut like with digital, the LED is pin is marked at ‘L’. Lets make the LED turn on very dimly.
AWrite('L', 10) # Set the LED 10% energy
We can use loops to fade the LED in and out.
@Loop For i=0 to 100 Step 10 AWrite('L', i) Wait(100) Next For i=100 to 0 Step -10 AWrite('L', i) Wait(100) Next Goto Loop
Reading an analog pin return the pin’s level. Run the following code and touch pin 0 with your finger.
@Loop a = ARead(0) # Read button 0 Println(a) Wait(100) # Repeat 10 times per second Goto Loop
Change the pin to ‘A’ and try again with the button. You are not seeing 0 and 1 like you did with Digital, are you?
Most systems sets analog outputs in a digital manner! Instead of very complex circuitry that generates a true analog value, system cheat a little and use a thing called Pulse Width Modulation. Using PWM, a pin can be turned on and off very quickly thousands or millions of times per second. When the pin is turned on half the time and off half the time, the average output power is half, as it was on only half the time.