# 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

## Python

``BrainPad.Analog.Write(4, 50)``

## C#

``BrainPad.Analog.Write(4, 50);``

## JS

``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.

## Python

``BrainPad.Analog.Write(BrainPad.Pin.Led,10)``

## C#

``BrainPad.Analog.Write(BrainPad.Pin.Led,10);``

## JS

``await BrainPad.Analog.Write(BrainPad.Pin.Led,10);``

We can use loops to fade the LED in and out.

## Python

``````level = 50
dlevel = 10
while True:
level = level + dlevel
if level<10 or level>90:
dlevel = dlevel * -1
``````

## C#

``````var level = 50;
var dlevel = 10;
while(true){
level = level + dlevel;
if(level<10 || level>90){
dlevel = dlevel * -1;
}
}``````

## JS

``````var level = 50;
var dlevel = 10;
while(true){
level = level + dlevel;
if(level<10 || level>90){
dlevel = dlevel * -1;
}
}``````

Reading an analog pin returns the pin’s level. Run the following code and touch pin 0 with your finger.

## Python

``````while True:

## C#

``````while(true){
}``````

## JS

``````while(true){
}``````

## BrainStorm

Most systems set analog outputs digitally! Instead of very complex circuitry that generates a true analog value, the system cheats a little and uses 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.