GPIO pins as inputs

381 words | 2018-3-26

So, let’s see how GPIO works on the input. Approximately one can imagine GPIO (input) as: GPIO input

Do not pay attention to the diodes, they still will not save. Resistor R1 can be connected as pull-up or pull-down or not at all, by the way, its resistance is quite approximately, it lies in the range of \(40k\Omega - 100k\Omega\).

To get rid of noise on the input line there is a Schmitt trigger (by default it’s active, you can turn it off, but it’s unclear why:smile:). Now the main thing:

  • You can apply 3.3V to the input even without pull-up and pull-down resistors and this is OK.
  • You must not apply a negative voltage!
  • You must not supply more than 3.3V!

Simple circuit with internal pull-up resistor

Let’s try using an internal pull-up resistor. Therefore, while the button is not pressed, a high level will be read. When the button is pressed, the low level will be read, let’s take this into account in the program. Scheme with internal pull-up resistor


Text of the program:

package io.github.yrabbit.kotlin

import jpigpio.JPigpio
import jpigpio.JPigpio.*
import jpigpio.Pigpio
import jpigpio.Utils
import kotlinx.coroutines.experimental.cancelAndJoin
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.runBlocking
import kotlin.system.exitProcess

fun main(args: Array<String>) {
    println("*** Raspberry Pi Kotlin ***")
    val pigpio = Pigpio()

    // Set pull-up resistor on button GPIO
    pigpio.gpioSetPullUpDown(, PI_PUD_UP)

    // Init LED gpio
    pigpio.gpioSetMode(, PI_OUTPUT)

    runBlocking {
        val job = launch {
            while (isActive) {
                with(pigpio) {
                    gpioWrite(, !gpioRead(
        // wait
        delay(8 * 1000)

    // clean up

fun normOutputDriveStrength(pigpio: JPigpio) {
    // Check drive strength on GPIO
    val strength = pigpio.gpioGetPad(0)

    if (strength != CURRENT) {
        pigpio.gpioSetPad(0, CURRENT)

 * Clean up
fun gpioSwitchToInput(pigpio: JPigpio) {
    UsedGPIO.values().forEach {
        pigpio.gpioSetMode(, PI_INPUT)

enum class UsedGPIO(val pin: Int) {
    ButtonPin(5),   // GPIO05
    LedPin(26)      // GPIO26

const val CURRENT = 8 // mA


The last push was at the time the program was completed:smiley:

External pull-up resistor

An external resistor has the advantage of not requiring a software turn-on and works right away. Scheme with external resistor


In the program, we just remove the pull-up line of the resistor: pigpio.gpioSetPullUpDown(, PI_PUD_UP)

How it works

Pull-down resistors

Scheme with pull-down resistor

There is no particular difference for amateur projects between pull-up and pull-down options, although I prefer the pull-up option. Cat in the wires