CONTROL INPUT AND OUTPUT ARDUINO

 HOW TO CONTROL INPUT AND OUTPUT ARDUINO

Tutotial arduino this time we try to learn about how the arduino pin works in input mode, input pullup and output In this arduino tutorial we use Arduino Uno, as we know arduino uno has as many as 14 digital pin input-output that is on pin 0 sd 13.


So every time we will use an arduino digital pin as input or as an output, we must first set the pin mode. This pin mode is set at void setup() by using the pinMode(pin, mode) syntax. Here we discuss each output input mode on the arduino.


Mode Input High Impedance

Syntax : pinMode(pin, INPUT);

note This high impedance input mode is the default or default mode of the arduino pin. In this mode it is as if there is a 100 Mega ohm resistor mounted series against the pin. This means that in these conditions pins need a slight current to change logic conditions, this mode is either used to read capacitive touch sensors or read LEDs as photodiodes.


Mode Input Pullup

Syntax : pinMode(pin, INPUT_PULLUP);

In this mode there is an internal resistor of 20K on the atmega arduino chip set through the program. This pull-up resistor is accessed by setting pinMode() as the INPUT_PULLUP. This means inversely proportional to high impedance input mode, HIGH logic means off condition and LOW logic means on.


So when using a pin as a pull-up input, the other end of the sensor component device or for example on the button must be connected to the ground. An example is used push button, if the button is not pressed or the condition is open then the arduino will read the high condition, and the LOW condition when the button is pressed. The following image shows an illustration of pull-up input.


Mode Output
Syntax : pinMode(pin, OUTPUT);
When the pin is set in output mode, the pin will be in a low impedance state. This means that pins can deliver sufficient current output to other circuits. Atmega pins can be sourced or synced by 40 mA (milliamper) to other circuits.

If the load flow from another circuit exceeds the current of the atmega pin, it can damage the pin, it can even damage the atmega ic. Therefore to overcome it it is recommended to attach a resistor 470 ohm or 1K ohm on the output pin to another circuit.

Reference arduino.cc/reference

After we discuss some input and output modes on the arduino, next we will create a program, which is to create replicas such as the spaceship interface or spacecraft control panel with led and push button.

Required components
1x Board Arduino
1x Breadboard
1x Green LED
2x Red LED
1x Pushbutton
3x Resistor 330 ohm
1x Resistor 10K ohm
Spaceship Interface Suite


explanation:
The key input mode used in the series above is high impedance input, hence a pull-down resistor is used on the button and on the other leg the button is connected with 5V. 

Why should there be a 10k resistor connected to the ground. Because if the resistor is not installed, then the reading on the digital pin will be floating or floating, the condition will sometimes logic high and then change to low so on and when the button is pressed, the program will not work properly.

Sketch Program ARDUINO IDE :

int tombol;
const int pin_led_hijau = 2;
const int pin_led_merah1 = 3;
const int pin_led_merah2 = 4;
const int pin_tombol = 5;
 
void setup() {
  pinMode(pin_led_hijau, OUTPUT);
  pinMode(pin_led_merah1, OUTPUT);
  pinMode(pin_led_merah2, OUTPUT);
  pinMode(pin_tombol, INPUT);
}
 
void loop() {
  tombol = digitalRead(pin_tombol);
 
  if (tombol == 1) {
    // Tombol di tekan
    digitalWrite(pin_led_hijau, LOW);
    digitalWrite(pin_led_merah1, LOW);
    digitalWrite(pin_led_merah2, HIGH);
    delay(250);
    digitalWrite(pin_led_merah1, HIGH);
    digitalWrite(pin_led_merah2, LOW);
    delay(250);
  } else {
    // Tombol tidak di tekan
    digitalWrite(pin_led_hijau, HIGH);
    digitalWrite(pin_led_merah1, LOW);
    digitalWrite(pin_led_merah2, LOW);
  }
}

Program description :
Note that line no 1 to 5 is the initialization of the global variable and specifies the pin number to use.
The int button variable is used to store the condition of the button.
In void setup() there is a pinMode() syntax to set the pin mode to be used as input or output.
The void loop() function is a continuous looping, the =digitalRead(pin_tombol) button serves to read the condition of the button associated with the pin and is stored on a key variable with a value of 0 for a condition not pressed 1 for the pressed condition.
On the 17th if line (button == 1) that is, if the button is pressed then run the program that is in it.
DigitalWrite(pin_led_hijau, LOW) syntax means to give the pin low (GND) logic and turn the green LED off.
On the 26th row else means that, apart from the above condition value that is the condition of the button variable, run the program inside.
DigitalWrite(pin_led_hijau, HIGH) syntax means to give the pin high(5V) logic and make the green LED light up.

1 Comments