What Are GPIO Pins Or General Purpose Input Output?

switchdoc labs gpio pin

What Are GPIO Pins Or General Purpose Input Output?

Today’s we are going to talk about GPIO Pins, the way to conquer the world. Well, at least to run the world.  

I always like to start out with a science joke. This neutron walks into a bar and sits down at the bar and he asked the bartender, well I’d like a beer how much? And the bartender looks at him and says, no charge … 😉

Back to our topic, GPIO Pins. GPIO stands for General Purpose Input Output. And today we’re going to talk about what are GPIO Pins, and what are some of the real uses. How to read values and some of the things you might want to control with GPIO Pins.

But first let’s start out with, what is a GPIO Pin?

They are General Purpose Input Output and are pins on various processors that aren’t dedicated to anything.  They’re designed for you to do input or output single bits.

A GPIO Pin is a single bit either input or output. gpio pins

Let’s talk about the kind of output pins you have. But first, let me give you just a feeling about how many people have GPIO Pins and what they’re used for. Here’s our famous Raspberry Pi. There are about 20 GPIO Pins up here. These GPIO Pins, operate 3.3 volts. More about that later. That’s on a Raspberry Pi. In an Arduino type product, you have even more GPIO Pins.

 

This is an Agpio pins arduinorduino Uno. And a lot of these pins here are General Purpose Input Output, even the pins down here which connect to the 10-bit Analog/Digital Converter that we talked about in our last blog. Those can also be used as General Purpose Input Output Pins. So there’s a lot of input-output on Arduino.

These two devices are really cool because you can control so many different things.

Here’s a board containing an ESP8266, another very popular chip. This is programmed like an Arduino in most cases, but it has a lot more memory than an Arduino. But unfortunately, it’s very, very short of GPIO pins. There’s only about maybe two or three that are available on this chip at all for use in General Purpose IO. Once you take away the serial port, once you take away the I2C interface, you’ll only have a couple of pins on there, and that’s not too cool. But anyway, we’ll come back to that now. But General Purpose Input Output pin first, let’s talk about Output. In the digital world, we have a signal above about 2.5 volts, it’s a 1 one is below 2.5 volts is 0, so I’m just going to call them  0’s and 1’s even though they’re different.

There are a new ESP8266 like chip out called the ESP32 which adds a second computer core and a whole bunch more GPIOs.   We will talk about that chip in the near future.

On a Raspberry Pi, as I mentioned a 1 will go up to about 3.3 volts. A 0 will be down near the ground, somewhere near 0 volts. On an Arduino, it’ll go to 5 volts and then go down to 0 volts. By the way, that means that these GPIO Pins aren’t really good to connect directly between an Arduino and a Raspberry Pi because you have 3.3 volts on one end and 5 volts on the other end, you could blow up the input-output pins on a Raspberry Pi.  It is tougher to do an Arduino, but you can still do it. So when you connect directly from an Arduino or Raspberry Pi, you need to plan for that. Anyway, an ESP8266 is also a 3.3-volt device so you can connect directly to a Raspberry Pi but not to an Arduino.

So we have to worry a little bit about what is a 1, what is a 0 on these parts. 5 volts, 3.3 volts. That’s the difference. Now, if I set the bit inside by using software to a 1, it’ll output that high voltage and that becomes a 1. Now you can do all sorts of these things with these bits. Well, it’s just one bit. Well, we’ll talk a little later about just the type of things you can do with these bits. Now let’s talk about Input. Input is a little trickier. Now, as I said above about 2.5 volts is 1, below 2.5 volts is 0. And that’s kind of the way the circuitry works. Not quite, but it’s pretty close to being that. Halfway between your power voltage and your ground voltage. So 3.3 Volt (like the Raspberry Pi)  might be a little lower than 2.5 volts at a 1.   if you’re at 5 volts its about 2.5 volts.

If the signal is a 1, if a digital output is outputting a 5 volts, let’s say, or 3.3 volts on the Raspberry Pi, your circuitry, will read it as a 1, which means 1, 0, 1, 0. That’s reading the input.

When you actually use an input on a GPIO, sometimes you have an output device that will only pull it down. It’s called an Open Drain and it’ll only pull something down. But it lets the input float if the device isn’t turned on. Well, you have to deal with that because if you let it float, you don’t know whether it’s going to end up being a 1 or a 0. These things just happen.  It can be a function of the humidity or temperature.  And it will change.

They move around, they float. That’s why it’s called floating these voltage levels will float. So what you do is put a Pull Up Resistor that might pull it up to 5 volts (or 3.3V for a Raspberry Pi), and then when the open drain turns on and pulls it down to 0, it’ll take that voltage all the way, very close to 0 hence a 0 digital value.

So we have Input, we have Output, we can do things with these Inputs and Outputs. In our last lecture, we talked about how we can turn an analog signal to a digital signal. Here we’re really talking about just reading digital signals in. But we can do some very interesting things with that. So let’s talk about what you use GPIO Inputs and Outputs for. Well, the simplest thing you ever think of is if you ever bring out an Arduino or a Raspberry Pi, you might do what we call a hardware “Hello World”. And what that is, is you make an led blink on and off. You do that by setting your pin to 1, 0, 1, 0. So you can turn it on and off, that’s using output. What about an input? You can hook up a switch out there that has that nasty little pull up to 5 volts or  3.3 volts.

Then you hit the button and it shorts, it takes it to ground, which makes it a 0 volt.

gpio pins

Now turns out, do you know mechanical switches bounce? They will read 1, 0, 1, 0, 1, 0, 1, 0 over maybe a hundred milliseconds. That’s a long time to a processor running 16,000,000 instructions a second. So we’ll talk about that in another lecture. But you can read switches. You can drive LED’s. You can do all these sorts of things by writing out, reading into your unit.

Basically, GPIO’s allow you to connect with the real world. That’s why we called people that hook up Raspberry Pi’s that they’re doing physical computing. They’re making the computers do something out in the real world. Whether it’s turning off a switch, whether it’s turning on an LED, whether it’s controlling a relay that turns the lights on your house or lights up a Christmas tree. All these things can be done with your little computer driving those GPIO Inputs and Outputs.

This really makes these little computers very flexible. We talked about controlling relays, controlling LEDs. So let’s talk about some of the other things you can use these for. This is a relay, it happens to be a Grove Connector, but it’s driven by a GPIO. The relay is on with a 1 and off with a 0. Now, this is actually called a Latching Relay and you end up using 2 GPIO Pins. One to turn it on and then one to turn it off and it stays latched, it stays on, stays off. Even if you turn the voltage off. So you know, that’s a special kind of relay, but it’s still really cool if you energize a relay with just a GPIO Pin, you can draw quite a bit of current, more about current in the future.

But these GPIO Pins can’t put out very much current, 5, 10, 15, 20 milliamps. You’re not going to drive something that needs a 1000 milliamps out of a GPIO. You have to put some kind of amplifier on it.  So 1 bit that’s not very interesting. Well, how do we get around that? Well, we’ll come back to that in a moment.

raspberry pi gpio pins

But first, let’s talk about this. Some of you guys may recognize this. This is actually an Ultrasonic Distance Detector and it works by using one Output GPIO and one GPIO Input. And how it works is you take the output and set it up to a 1 and then you’re listening to the Input and depending because your computer is so fast, you can measure the time interval.

It took the distance to get from here one point to another, in this case, sound from one point to another. You’ve got the distance that can be measured. You can use this for robots, you can use it for people detection, all sorts of different things. 2 GPIO Pins, one Output to send a pulse out and one Input to read when that pulse comes back. When you have the time between those two things. You’ve got the distance. Now, what about some other devices. You guys have probably heard about Serial Devices? Serial Devices only use one Input, but you transmit sequences that are timed so you try 1, 0, 1, 0, 1, 0, and you do that in a certain way you know it’s a byte of data. So you’re using one Input, one Output on a serial device, but you can send lots and lots of information by using the timing to send 1, 0, 1, 0, 1, 0, and so on, and then you can receive data back the same way.

You’ve got a couple of issues with that. For one thing, you need to know when the bytes start, not so much with the bytes quit, but we need to know when the bytes start. That’s called Framing and it’s beyond our talk today, but there’s a number of very easy ways to do that. And so we can communicate lots of information through these serials. Speaking of serial signals, you know USB right? USB stands for Universal Serial Bus. There’s only one Input, one Output. Actually it’s bi-directional, but in any case, there’s one Input, one Output, and it serial sending data in and out that way. Just like a GPIO Pin except it’s done very cleverly so you can get a lot of data across that. One of the things we talked about was this little device here, this ESP8266 that doesn’t have very many Input/outputs on it, which means you can’t do as many fun things with a little device.

switchdoc labs gpio pin

Well, there are solutions to this. Here’s a particular device. This is called a GPIO Extender, and you can stick this on a Raspberry Pi, an Arduino or an ESP8266. This is a product we sell at SwitchDoclabs.com and this adds 8 GPIO signals to your device. You can hook up to a Raspberry Pi, an Arduino or even the ESP8266 that only has a few different GPIOs on it. These GPIO’s take a little more programming, but you can Interrupt. There is a scary word Interrupts. We’ll have to have a talk about Interrupts in the very near future, but you can make these interrupt your processors so when one of them changes you know what it is. You have to use what’s called an I2C Bus here, which is a bus that’s used to connect back and forth to various sensors and devices more on that in the future.

You could have just as many GPIO’s as you want on any device by using one of these little extenders. Let’s summarize. GPIO stands for General Purpose, Input, Output. All the processors you use to have at least a few, a Raspberry Pi and an Arduino have a lot of General Purpose Input Output that you can design your circuits and you can read your sensors off of. Very much like our friend, the little Ultrasonic Detector that you can put it in your little robot and you can use a GPIO to drive those. You can use GPIO’s to drive LEDs. You can use GPIO’s to read data, serial and others coming back from other devices like the Ultrasonic Detector, but there are hundreds of other devices that can do that too.

So why do we want GPIO’s? Because we want to control the world and that’s always a good thing.

At least I think it’s fun. I like GPIO’s, I use them a lot. Granted I do a lot of LED’s and make pretty lights and everything. But I also use it in robots. We use Ultrasonic Detectors in robots all the time. We use all sorts of different pulses. And then there’s something called a LiDAR. What’s a LiDAR? Well, it’s a Laser Ultrasonic Detector basically, but it’s using light instead of sound. But you’re still measuring the time it takes light to get out and back. You’re probably not doing that with your 16 megahertz processor, but they have a processor built in that’s fast enough to measure those distances or a piece of hardware. So you get data back in serially to you saying, there are all sorts of stuff out there. That’s how they build self-driving cars by the way. They use LiDAR, they’re kind of like really fancy ultrasonic devices.

That’s it for our GPIO talk today.  Now go out and conquer the world!

Please don’t hesitate to email us questions at SwitchDoc Labs or if you have suggestions for topics, let us know. We’ll be happy to do them.