Understanding GPIO analog and digital

A GPIO pin is a 'general purpose input/output' pin. This is by default only high or low (voltage levels, high being the micro controller's supply voltage, low usually being ground, or 0V). But the levels of 'high' and 'low' are usually given as voltages as a proportion of the supply voltage. So anything usually above 66% of the supply voltage is considered a logic level 'high' which means some lower voltage devices can talk with high voltage devices as long as the levels fall within what is considered 'high'. A 1.8–2.7V low power microcontroller or GPS receiver for example will have trouble communicating directly to a 5V microcontroller because what the low voltage device sees as 'high' the higher voltage device will not think it's high at all. This is for using GPIO as an input pin, and output is basically the same - the output high is based on the supply of the controller, where it will drive current out and set the voltage of that pin to VCC, or sink current and pull the pin to 0V for a logic 'low'.

Sometimes you can use a SINGLE pin for 'analog' values, by configuring the GPIO pin to be used by other onboard devices like an 'analog to digital' (ADC) converter. The pin is set to a channel on the ADC and this acts as an input to the ADC now, not a normal GPIO pin. You can then set the ADC to take a sample, and read the ADC's result register value for numbers like 0-1024 if it's 10-bit resolution.

As someone has mentioned, a GPIO pin could be used in software to give the effect of a Pulsed Width Modulation (PWM) signal, usually at low speeds for GPIO toggling. Most microcontrollers have dedicated PWM generators which can be configured to use a GPIO pin as an output pin, and these are very fast and far more stable than using software to control GPIO for generating a PWM signal. PWM are used for 'average' or '%' style signals and allow you to do things like dim lights and control a motor's speed.

GPIO pins are usually arranged in groups, called Ports. In small controllers, they might be 8-bit architecture, so ports are often grouped into lots of 8, and their values can be read all at the same time by reading a 'data register' that represents the logic high/low values of those pins. Similarly, you can set pins to be outputs and then write 8-bits into a data register, and the microcontrollers GPIO controller will read the register's changed values, and drive the pin high or pull the pin low depending what value you just set.

In newer controllers such as the ARM Cortex A8 and A9 like in the Raspberry Pi and BeagleBone, their GPIO controllers and different options are very complicated. They use a 32-bit architecture, so most GPIO pins are arranged into 32-pin blocks, even if not all are actually usable (some might be dedicated or not enabled). The BeagleBone (which I have worked on before) has some really awesome options for its large amount of pins, and sometimes you will need to use a 'pin mux' tool, which allows you to set up the special modes of certain pins for things like PWM, pulse capture, timer outputs, analog (ADC) channel inputs, and even (on the BeagleBone anyway) mapping to the industrial sub-processors available on the ARM core, but are considered independent processors and need their own pin mapping in order to be connected to the outside world.


You are most likely referring to Arduino's analog out, which often uses a GPIO pin with software PWM. GPIO typically have three states. Output High, Output Low, and Input/High-Z (High Impedance, where it doesn't affect the output).

PWM rapidly toggles an output from Output High to Output low (period), to create an average (Duty Cycle), allowing for something that looks like an analog value. By toggling a Binary GPIO at a 50% (or 128) duty cycle, the output is still binary, but averages out to half way between High and Low.

Think of a light bulb. You see it On, or Off. But it's really turning on and off 60 times per second, so fast that you don't notice its blinking really fast. But turn the light bulb on and off manually really slowly, and you notice its blinking. By 255, it means 100% on, and less than 255 is a fraction of 100% on.

That's how a Binary GPIO can act like a 255 state Analog pin.

Tags:

Gpio