12
\$\begingroup\$

I'm trying to understand GPIO, and have read a bunch of different blog posts and I think I'm close to getting it, but still struggling with a few things.

I've seen a few reference to GPIO only being able to work with binary values, but other posts which say the value can be from 0-255. I'm assuming this is the difference between analog and digital GPIO. Is that correct? Is it possible that a single GPIO pin can act as both analog and digital?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ As a follow up to some of the answers... The binary value refers to a digital input or output. It can be a 1 (which should be the source voltage, called "hi" or "on") or 0 (meaning 0V, called "lo" or "off"). That is 1 bit resolution. An analog input can be a range, typically from 0V to VCC (the source voltage). A digital chip with 8bit resolution will convert this analog input to an 8bit digital signal of range 0 - 255 (binary: 00000000 - 11111111), because 2^8 - 1 = 255 (binary means base 2, the 8 is from the 8 bit part, and "- 1" because digital stuff starts counting at 0, not 1). \$\endgroup\$ Mar 28, 2014 at 1:36

3 Answers 3

14
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Could you give an example how "SINGLE pin can be used for 'analog' values"? \$\endgroup\$
    – Pavel_K
    Oct 31, 2015 at 11:03
  • \$\begingroup\$ @JimJim2000 just a stream of on/off or proper PWM through an RC filter to produce a smooth analog "average" voltage on the output. \$\endgroup\$
    – KyranF
    Nov 2, 2015 at 1:03
7
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks @Passerby, that's much clearer. I'm not specifically looking at arduino, but raspberry pi and beaglebone. Does what you're saying mean that any pin can have a value between 0 and 255? I'm getting confused between documentation that says a value can be binary vs others that say a value can be 0-255. \$\endgroup\$
    – pedalpete
    Mar 28, 2014 at 1:05
  • 1
    \$\begingroup\$ @pedalpete the RPI and Arduino have alot of overlap in crowd and ideas. But A PWM of 0 to 255 is arbitrary. It can be 0 to 65535, or 0 to 16. 0 to 255 is 8 bits of precision. Where 255 or 65535 or whatever is 100%. A GPIO only has Binary On and Off, but you use PWM to pretend it can have more than just on or off. It's a useful, and intentional optical illusion. \$\endgroup\$
    – Passerby
    Mar 28, 2014 at 1:49
  • 2
    \$\begingroup\$ A light bulb doesn't blink on and off at 60 times per second. The filament heats up and stays heated up \$\endgroup\$ Mar 28, 2014 at 10:39
0
\$\begingroup\$

A GPIO bit can be 0 or 1. An 8 bit GPIO port, made of 8 consecutive bits, can range between 00000000 and 11111111, or 255 decimal. This has little to do with the concept of "analog"

\$\endgroup\$
2
  • \$\begingroup\$ Thanks Scott, though I think I'm more confused now than before. What's a GPIO port? If I have a GPIO pin (example on a Raspberry Pi), is each pin considered a port? If so, is each pin able to work with the said range 0-255 decimal? \$\endgroup\$
    – pedalpete
    Mar 28, 2014 at 1:02
  • \$\begingroup\$ Ignore my last question, I see what you now mean about a port. \$\endgroup\$
    – pedalpete
    Mar 28, 2014 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.