Analog Signals
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.
Analog Output
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 true and false in coding. To set an LED to be half brightness, for example, we can use the Analog.Write() function and set the output to 50, which is 50%.
Set pin 4 to analog at 50% energy
await BrainPad.Analog.Write(4, 50);
Jut like with digital, the LED is pin can be used. Let’s make the LED turn on very dimly.
await BrainPad.Analog.Write(BrainPad.Pin.Led,10);
We can use loops to fade the LED in and out.
var level = 50;
var dlevel = 10;
while(true){
level = level + dlevel;
if(level<10 || level>90){
dlevel = dlevel * -1;
}
await BrainPad.Analog.Write('l',10);
await BrainPad.System.Wait(100);
}
Analog Input
Reading an analog pin return the pin’s level. Run the following code and touch pin 0 with your finger.
while(true){
var a = await BrainPad.Analog.Read(0);
await BrainPad.System.Println(a);
await BrainPad.System.Wait(100);
}
BrainStorm
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.