Learn electronics, coding, and projects — step by step.

Tutorial: Arduino GPIO | How to use Arduino Pins

George Bantique June 26, 2020 No Comments

Table of Contents

When I started learning the Arduino microcontroller, I searched for ways to use it immediately because I believe that the best way of learning something is by experiencing it firsthand.

In this tutorial, we will learn the basics of Arduino GPIO — General Purpose Input/Output, or in simpler terms, Arduino pins.

GPIO are physical pins found in microcontrollers which can be configured either as input or output.

When a certain pin is configured as input, it can be used to read the state of a switch or the value of a sensor.

When a certain pin is configured as output, it can be used to control devices — such as turning lights ON or OFF or controlling the rotation of DC motors.

Now let us discuss some functions commonly used in Arduino:

// Setting of the pin direction
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YEL_LED_PIN, OUTPUT);
pinMode(SERVO_LED_PIN, OUTPUT);

pinMode(JS_YL_PIN, INPUT);
pinMode(JS_XL_PIN, INPUT);
pinMode(JS_YR_PIN, INPUT);
pinMode(JS_XR_PIN, INPUT);

pinMode(JS_SL_PIN, INPUT_PULLUP);
pinMode(JS_SR_PIN, INPUT_PULLUP);

pinMode(PIN, MODE) — This function sets the direction of the pin, either as input or output.

MODE could be OUTPUT, INPUT, or INPUT_PULLUP. For example:

pinMode(13, OUTPUT) — This configures digital pin 13 as an output.

  • OUTPUT mode configures the pin as an output, which can drive LEDs, LCDs, control signals, etc.
  • INPUT mode configures the pin as an input, which can read the state of a switch or sensor data. An external pull-down or pull-up resistor is needed when the input pin has an unknown state (tri-state).
  • INPUT_PULLUP mode configures the pin as an input and connects it to an internal pull-up resistor. This is useful when no external pull-up or pull-down resistor is available.

Pull-up Resistor — a resistor connected between VCC and a specific pin. It makes the pin default to logic HIGH. Example: the RESET button of Arduino Uno uses a pull-up resistor, so its default value is HIGH, but when pressed it connects to GND (logic LOW).

Pull-down Resistor — a resistor connected between GND and a specific pin. It functions like a pull-up resistor but in the opposite logic (default LOW).

digitalWrite(PIN, HIGH/LOW) — drives the pin either logic HIGH or LOW.

digitalRead(PIN) — reads the state of the pin and returns either logic HIGH or LOW.

analogWrite(PIN, VALUE) — writes an analog value (PWM square wave) to a pin.

analogRead(PIN) — reads an analog value from a specified analog pin.

VIDEO DEMONSTRATION

SOURCE CODE


#include "Servo.h"
#include "LiquidCrystal.h"

#define RED_LED_PIN 11
#define YEL_LED_PIN 12
#define SERVO_PIN   A5

#define JS_YL_PIN   A1  // Joystick vertical position in the left
#define JS_XL_PIN   A2  // Joystick horizontal position in the left
#define JS_YR_PIN   A3  // Joystick vertical position in the right
#define JS_XR_PIN   A4  // Joystick horizontal position in the right

#define JS_SL_PIN   3   // Joystick tactile switch in the left
#define JS_SR_PIN   2   // Joystick tactile switch in the right

Servo myservo;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {

  // Setting of the pin direction
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(YEL_LED_PIN, OUTPUT);
  pinMode(SERVO_PIN, OUTPUT);

  pinMode(JS_YL_PIN, INPUT);
  pinMode(JS_XL_PIN, INPUT);
  pinMode(JS_YR_PIN, INPUT);
  pinMode(JS_XR_PIN, INPUT);

  pinMode(JS_SL_PIN, INPUT_PULLUP);
  pinMode(JS_SR_PIN, INPUT_PULLUP);

  // ##########################
  myservo.attach(SERVO_PIN);  // Attach the servo to the assigned pin
  lcd.begin(16,2);            // Initialize the LCD as 16 columns and 2 rows
  lcd.clear();                // Clear any garbage in the LCD EPROM
  
}

void loop() {

  //#######################################
  // Using the horizontal (X) analog value
  int js_xr_value = analogRead(JS_XR_PIN);
  byte servo_angle = map (js_xr_value, 0, 1023, 0, 180);
  myservo.write(servo_angle);
  lcd.setCursor(0,0);
  lcd.print("Servo angle: ");
  if (servo_angle < 100) {  // if value is 0 to 99, add a space before printing
    lcd.print(" ");         // this is to make sure that 3 digits is occupied always
  }
  lcd.print(servo_angle);

  //#######################################
  // Using the vertical (Y) analog value
  int js_yr_value = analogRead(JS_YR_PIN);
  byte led_pwm = map ( js_yr_value, 0, 1023, 255, 0);
  analogWrite(RED_LED_PIN, led_pwm);

  lcd.setCursor(0,1);
  lcd.print("LED pwm: ");
  if (led_pwm < 100) {  // if value is 0 to 99, add space before printing
    lcd.print(" ");     // this is to make sure that 3 digits is occupied always.
  }
  lcd.print(led_pwm);

  //#################################################
  // Using the push button switch (S) digital value
  digitalWrite(YEL_LED_PIN, digitalRead(JS_SR_PIN));
  
}

CALL TO ACTION

That's all for now, I hope this tutorial helps. Please kindly LIKE and SHARE this video to your friends who may benefit from it.

SUBSCRIBE and leave your comments and suggestions in the comment box.

Thank you and have a good day.

George signing off, bye.

×

Leave a Reply

Required fields are marked *






Related Articles: (by Categories)