Skip to main content

Analog Outputs

Let's see how we can generate analog signals, which are signals that can have several intensities between 0 and 1 (0 volts and 3.3 or 5 volts). Real analog signals can have an infinite number of values, but since we are using MCUs, we are in the digital world and can only have a finite number of values.

  • Add an Analog Out module (the Output group), drag it into the interface, and then configure it by selecting pin D13. If you're using a Teensy and you've selected "Teensy" as your board, you won't find pin D13. This is normal, as Teensy boards don't have a PWM feature on pin D13. Therefore, you won't be able to use this method for these boards (so skip the next method).

  • To the left of this module, add another Generator module (Synth group, at the very bottom of the sidebar), set the type to Sine and the frequency to 1 Hz. Leave the other settings at their default values.

  • Connect the output of the Generator module to the Analog Out module input.

You should now have this:

Upload the code: you should have the LED intensity slowly changing, instead of just being on or off.

If you're using a Teensy, or if you want to implement this example on any pin on any board, you'll need to use another "Generator" module, configured in PWM mode. This module actually performs PWM in software, so it won't be able to go as high in frequency as hardware PWM (which can go beyond 20 kHz, or even higher on some boards). We'll see later how to use PWM to control motors.

  • Delete the Analog Out module.

  • Add another Digital Out module and configure it by selecting pin D13 or the one with an LED on the board you are using.

  • Between this module and the Generator module created earlier, add another Generator module, rename it PWM, then set its type to PWM and its frequency to 100 Hz. Leave the other settings as default.

  • Connect the output of the Generator module to the Duty input of the PWM module, and the output of PWM to the Digital Out input.

You should now have this:

Upload the code. You should have an LED that gently blinks continuously, instead of just being on or off.

Thanks to all these LED examples, you've been able to discover a few Node Blue modules. You should be starting to get a feel for the spirit of this tool and the infinite possibilities it offers.

But these examples are more than just examples. You'll see in the following tutorials that we'll often use LEDs to test processes, as they allow you to visualize a Boolean state (0 or 1), but also an "analog" quantity by varying their intensity.

A quick aside on the term "Analog."

I put quotation marks around "Analog" because it's a bit of a misnomer here. In the Arduino environment, the "analogWrite" function uses, with some exceptions, the boards' hardware PWMs, at a frequency generally between 500 and 1000 Hz. Node Blue's "Analog Out" module uses this function.

When this type of signal is applied to a LED, the persistence of vision in our eyes gives us the impression that the intensity can be continuously adjusted between 0 and 255 (this is the range of values that analogWrite can use). However, if you connect this signal to an amplifier to drive a motor or a speaker, you will hear a whining noise at the PWM frequency. This is also why we try to increase the PWM frequency above 20 kHz when driving motors, because our ears generally cannot hear beyond that frequency (except for the very young, the older we get the more the maximum frequency we perceive decreases). Class D audio amplifiers also use this technique because it allows for very good efficiency and lower manufacturing costs compared to Class A, B, or C amplifiers.

On Teensy, SamD21 and many more recent micro-controllers, there are one or more DAC (digital to analog converters), which are more properly called "Analog" because they do not produce any unwanted frequencies (they do but much less). Of course, they also have their limitations, as they necessarily have a finite resolution (generally 8 to 24 bits), can exhibit non-linearities, and if they are powered by a noisy voltage source, the output will be noisy too. But if you want to record audio, or if the interference frequencies generated by the PWM are a problem, you will need to use DACs.

We will now continue our exploration of modules and discover digital inputs.