Analog control through code

The most valuable part of this example is not its physical part but its logical one, and I'll show you the associated functions to deal with analog outputs and a very important control structure in any programming language called a for loop in the code we will be using for this circuit.

The analogWrite() function

Dealing with analog outputs from the programming side is just a matter of using the analogWrite() function, which, as its name implies, writes an analog value through a pin.

It takes two parameters, similar to the digitalWrite() function:

  • The first one is the pin on which we want to operate (remember that it has to be one marked as PWM) on
  • The second parameter is the value we want to output, and that, as I told ...

Get Arduino Essentials now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.