Lesson-03 Input

Introduction with Example

Input pins are essential in the Arduino Uno as they allow the device to receive information from external sensors, switches, buttons, and other input devices. By using input pins, the Arduino Uno can respond to changes in the environment or user input and perform specific actions or make decisions based on that input.

Here’s an interesting example of the need for input pins in Arduino Uno:

Let’s say you want to build a smart garden that automatically waters your plants when the soil is too dry. You could use a moisture sensor that detects the moisture level in the soil and sends that information to the Arduino Uno through an input pin. The Arduino Uno could then use that information to determine if the soil needs to be watered and activate a water pump connected to an output pin to water the plants.

Without input pins, the Arduino Uno would not be able to detect the moisture level in the soil, and the smart garden would not function as intended.

Another example could be a security system for your home that uses a motion sensor to detect movement and triggers an alarm connected to an output pin. Without input pins, the Arduino Uno would not be able to receive signals from the motion sensor, and the security system would not work.

Overall, input pins are a critical component of the Arduino Uno, as they allow the device to receive information from external sources and make decisions or perform specific actions based on that input.

Concept of PULL-UP & PULL-DOWN Resistors

In Arduino Uno, input pins are equipped with a built-in pull-up resistor. When an input pin is in a floating state, meaning there is no input device connected to it, the pull-up resistor keeps the input pin at a HIGH state by default. This prevents the input pin from floating between HIGH and LOW states, which can cause false readings and introduce noise into the system.

However, in some cases, it may be necessary to use an external pull-up or pull-down resistor to improve the reliability and accuracy of the input signal. This is especially true when dealing with noisy or unstable input signals.

A pull-up resistor is a resistor that is connected between the input pin and the power supply (VCC). This resistor ensures that the input pin is always in a known state, even when the input device is not connected or is disconnected. When the input device is connected and closes the circuit, the input pin detects the change in voltage and switches to a LOW state.

On the other hand, a pull-down resistor is a resistor that is connected between the input pin and ground (GND). This resistor keeps the input pin at a LOW state when the input device is not connected, and ensures that the input signal is stable and free from noise.

To use external pull-up or pull-down resistors with Arduino Uno, the input pin must be configured as an input and the appropriate resistor must be connected to the pin. The resistor value should be chosen based on the specific requirements of the input device and the input circuit.

Overall, understanding the concept of pull-up and pull-down resistors is essential for improving the reliability and accuracy of input signals in Arduino Uno projects, especially when dealing with noisy or unstable signals.

Example

  1. Button input with pull-up resistor:

Suppose you want to use a button to control an LED in your Arduino project. You can connect one end of the button to the input pin and the other end to ground, and add a pull-up resistor between the input pin and VCC. When the button is not pressed, the input pin will read a HIGH state due to the pull-up resistor. When the button is pressed, the input pin will read a LOW state.

Here’s the circuit diagram:

  1. Sensor input/Button with pull-down resistor:

Suppose you want to use a temperature sensor that outputs a voltage signal to an analog input pin on your Arduino. To ensure stable and accurate readings, you can add a pull-down resistor between the input pin and ground. The value of the resistor should be chosen based on the input impedance of the sensor and the desired sampling rate. we can also connect button input with pull-down resistor as we did in above example.

Here’s the circuit diagram:

Overall, using pull-up and pull-down resistors with input pins in Arduino projects is a simple and effective way to ensure reliable and accurate input signals.

Here is the code example with two buttons connected on pin no 2 & 3 with pull up and pull down resistors respectively.

int buttonPin1 = 2; // button connected to pin 2
int buttonPin2 = 3; // button connected to pin 3

void setup() {
  pinMode(buttonPin1, INPUT_PULLUP); // enable internal pull-up resistor for pin 2
  pinMode(buttonPin2, INPUT_PULLDOWN); // enable internal pull-down resistor for pin 3
  Serial.begin(9600); // initialize serial communication
}

void loop() {
  int buttonState1 = digitalRead(buttonPin1); // read state of button connected to pin 2
  int buttonState2 = digitalRead(buttonPin2); // read state of button connected to pin 3

  if (buttonState1 == LOW) { // if button connected to pin 2 is pressed
    Serial.println("Button 1 pressed!"); // print message to serial monitor
  }

  if (buttonState2 == HIGH) { // if button connected to pin 3 is pressed
    Serial.println("Button 2 pressed!"); // print message to serial monitor
  }

  delay(100); // wait for 100ms
}

In this example, the INPUT_PULLUP and INPUT_PULLDOWN options are used to enable the internal pull-up and pull-down resistors for the input pins.

Note that the delay() function is used to add a small delay between readings to debounce the buttons and avoid false readings caused by button bouncing.

Step by Step Explanation

here’s an explanation of the code:

  1. In the first two lines, two integer variables buttonPin1 and buttonPin2 are declared and assigned the values of the pins that the buttons are connected to.
  2. In the setup() function, the pinMode() function is used to configure buttonPin1 as an input with a pull-up resistor enabled and buttonPin2 as an input with a pull-down resistor enabled.
  3. The Serial.begin() function initializes the serial communication.
  4. In the loop() function, the digitalRead() function is used to read the state of the buttons connected to buttonPin1 and buttonPin2 and assign them to the buttonState1 and buttonState2 variables respectively.
  5. The if statements check if the buttons are pressed, based on the state of the buttonState1 and buttonState2 variables.
  6. If the button connected to buttonPin1 is pressed, the message “Button 1 pressed!” is printed to the serial monitor.
  7. If the button connected to buttonPin2 is pressed, the message “Button 2 pressed!” is printed to the serial monitor.
  8. The delay() function pauses the program for 100 milliseconds before looping again.

Inbuilt Functions

  1. pinMode(): This function sets the mode of a pin as either an input or an output pin. It takes two parameters – the pin number and the mode (INPUT or OUTPUT).
  2. digitalRead(): This function reads the state of a digital pin (either HIGH or LOW) and returns the value as an integer. It takes one parameter – the pin number.
  3. analogRead(): This function reads the value of an analog pin (between 0 and 1023) and returns the value as an integer. It takes one parameter – the pin number.
  4. attachInterrupt(): This function attaches an interrupt to a pin and specifies the function to be executed when the interrupt is triggered. It takes three parameters – the interrupt number, the function to be executed, and the mode (RISING, FALLING, or CHANGE) that triggers the interrupt.
  5. pulseIn(): This function measures the duration of a pulse on a pin (either HIGH or LOW) and returns the duration in microseconds as an integer. It takes two parameters – the pin number and the state to measure (HIGH or LOW).

    Leave a Reply

    Your email address will not be published.

    Need Help?