Arduino – Inputs and Outputs

The legs of Arduino cards can be configured as digital or analog inputs or outputs, and there are various communication ports. Arduino analog pins can be used as digital pins.

pinMode () function

The pinMode () function can be used to configure a specific pin to act as an input or output. Syntax of the pinMode () function:

pinMode(pin, mode)

pin  – the number of the pin whose mode you want to set. The data type is  int .
mode  – this can be INPUT, OUTPUT or INPUT_PULLUP.

Arduino pins are inputs by default, so it is not necessary to declare them as inputs, but it is better to do so using the pinMode () function.

pinMode (pin, INPUT);

Pins with an INPUT configuration to which nothing is connected can be affected by electrical noise from the environment that can generate unexpected changes in the state of the pin. For stability, must be installed a pull-up resistor. Using pull-up resistors, the input is HIGH by default. This can be done by connecting a 10K resistor between the input and 5V. INPUT mode disables the use of the internal pull-up resistor.

In INPUT mode we can also use a pull-down resistor, this can be done with a 10K resistor connected to ground, then our input will be low by default.

You can also use the internal pull-up resistor, (INPUT_PULLUP), in which case the input will be high by default.

pinMode(pin, INPUT_PULLUP);

You can use pinMode () to configure the pins as output (OUTPUT). Atmega pins can provide up to 40 mA (milliamperes) of current. This is enough current to illuminate an LED or operate a transistor, but there is little to operate relays or motors. Connecting high current devices to the output pins can damage the pin output transistors or damage the entire Atmega chip. You can help protect your pins by connecting the OUTPUT pins to other devices through a 470Ω – 1k resistor.

digitalWrite () function

If the pin is configured as OUTPUT, you can use the  digitalWrite ()  function to write HIGH or LOW to it. In this case, the pin voltage, if the HIGH parameter is specified, will be set to the operating voltage of the board (5 V or 3.3 V). If LOW is used, the pin voltage will be 0V.

If the pin is configured as INPUT, digitalWrite () enables (HIGH) or disables (LOW) the internal pull-up resistor of the input pin. Instead, use the pinMode (pin, INPUT_PULLUP) function to enable the internal pull-up resistor. Syntax of the digitalWrite () function:

digitalWrite(pin, VALUE);

pin  – the number of the pin whose mode you want to set.
VALUE  – HIGH or LOW.

Let’s see an example. (Pin 13 is the on-board led.)

int Led = 13;

void setup()
{
  pinMode(Led, OUTPUT);
}
void loop()
{
  digitalWrite(Led, HIGH);
  delay(1000);
  digitalWrite(Led, LOW);
  delay(1000);
}

digitalRead () function

The Arduino can detect if there is voltage at one of its contacts, this can be read using the digitalRead () function. 

If the pin is configured as INPUT or INPUT_PULLUP, you can use the  digitalRead ()  function to read the status of the pin. This can be HIGH or LOW.

For 5V boards, a voltage greater than 3.0 V returns a high logic value, and a voltage less than 1.5 V returns a low logic value.

For 3.3 V boards, a voltage greater than 2.0 V will be high, while a voltage less than 1.0 V will be a low logic value. Syntax of the digitalRead () function:

digitalRead(pin);

pin  – The number of the pin you want to read about.

An example. When the button is pressed, the LED lights up:

int Led = 13;
int Button = 2;

void setup()
{
  pinMode(Led, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
}
void loop()
{
  bool ButtonValue = digitalRead(Button);
  
  if(ButtonValue == true)
  {
    digitalWrite(Led, HIGH);
  }
  else
  {
    digitalWrite(Led, LOW);
  }
}
Arduino UNO, Pushbutton and LED

analogRead () function

The value of an analog sensor is constantly changing. To read these types of sensors, we need an input that can handle this.

On the Arduino board you will find inputs labeled “Analog In”. These special pins tell you not only if there is voltage on them, but also its value. The  analogRead ()  function can be used to read the  pin voltage.

This function returns a number between 0 and 1023, which represents a voltage between 0 and 5 volts. For example, if we read 2.5 V from pin A0, the value of analogRead (A0) is 512. The syntax of the analogRead () function is:

analogRead(pin);

pin  – the number of the analog pin you want to read about. (This is on most boards, eg UNO: A0 – A5, for Mini and Nano A0 – A7, if the chosen card is Mega then A0 – A 15 pins.)

In the example below, the value of a potentiometer is read and sent on the serial port.

int potmeterPin = A0;
int potmeterValue = 0;

void setup()
{
  Serial.begin(9600);
} 

void loop()
{
  potmeterValue = analogRead(potmeterPin);
  Serial.println(potmeterValue);
  delay(100);
}

analogReference () function

Sets the reference voltage used for the analog input. This is the upper value of the input range. The analogReference function returns a number between 0 and 1023. Syntax of the analogReference () function:

analogReference(type);

type – you can use any of the following types:

DEFAULT  – The default analog reference. Operating voltage of Arduino boards (5V or 3.3V).

INTERNAL  – Built-in reference, 1.1 on ATmega168 or ATmega328 and 2.56 on ATmega8 (Arduino Mega not available)

INTERNAL1V1  – Built-in 1.1 V reference (Mega only)

INTERNAL2V56  – Built-in 2.56 V reference (Mega only)

EXTERNAL – The voltage applied to the AREF pin (0-5 V only) is used as a reference.

Only values ​​between 0V and 5V can be used for the external reference voltage of the AREF pin. If you are using an external reference on the AREF pin, you must set the analog reference to EXTERNAL using the analogReference () function in the setup () section before calling the analogRead () function. Failure to do so we can connect the active internal reference voltage and the AREF pin and this can damage the microcontroller.. 

The AREF pin on the Arduino UNO card
The AREF pin on the Arduino UNO card
Arduino Uno AREF EXTERNAL example
Arduino Uno AREF EXTERNAL example

In the circuit shown in the figure above, we set the AREF voltage to 3.3V using a voltage divider. This will be the upper limit of the measurable value. Connect the center terminal of the potmeter to pin A0, the right to GND, and the left to 3.3V. And an example code:

int potmeterPin = A0;
int potmeterValue = 0;

void setup()
{
   Serial.begin(9600);
   analogReference(EXTERNAL);
}

void loop()
{
   potmeterValue = analogRead(potmeterPin);
   Serial.println(potmeterValue);
}

You can see the value set by the potmeter on the serial console.

analogWrite () function

The analogWrite () function writes an analog value to the appropriate pins. This is a constant rectangular sign with a fill factor according to the value specified in the parameter.

The analogWrite () function can be used, for example, to change the brightness of LEDs, to control the speed of motors, and so on. 

TABLEPWM PINSPWM FREQUENCY
Uno, Nano, Mini3, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)
mega2 – 13, 44 – 46490 Hz (pins 4 and 13: 980 Hz)
PWM pins can be used on Arduino boards

Syntax of the analogWrite () function:

analogWrite(pin, VALUE);

pin – The number of the pin you want to write to. (see table above for suitable pins)
VALUE – duty cycle (fill factor): 0 -255.

Let’s look at an example:

int ledPin = 9;
int potmeterPin = A0;
int potmeterValue = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  potmeterValue = analogRead(potmeterPin);
  analogWrite(ledPin, potmeterValue / 4);
}

Read the potentiometer value with the analogRead () function, then divide the value by four and print it to the ledPin using the analogWrite function.

However, the incoming value must be divided by four because the value returned by the analogRead function can be between 0 and 1023. However, you can only specify a value between 0 and 255 for the analogWrite function.


Perhaps the most important means of communicating with arduino boards is the serial port. Let’s see how it works.

Serial.begin ()

Before you can use serial communication on your arduino board, you must initialize the serial port. Serial.Begin () sets the data rate in bits per second (baud) for serial data transfer. This should be done in the void setup () section.

// Serial.Begin(baud);

void setup()
{
  Serial.Begin(9600);
}

Serial.print ()

It prints the data in human readable form as ASCII text on the serial port. Numbers are printed in ASCII characters for each digit. Float types are printed similarly to ASCII digits, up to two decimal places by default. The bytes are sent as a single character. Characters and strings are sent as they are.

void setup()
{
  Serial.Begin(9600);

  Serial.print(40);              // prints 40
  Serial.print(29.2236);         // it only prints 29.22, up to two decimal places  
  Serial.print('H');             // the H character
  Serial.print("Hello World");   // and a string: Hello World
}
void loop()
{

}

Optionally, you can specify a second parameter to format the output.

void setup()
{
  Serial.Begin(9600);
  
  Serial.print(100, 0);   // Prints according to ASCII Table: 40
  Serial.print(40, BIN);  // Prints according to ASCII Table: 0110 0100 
  Serial.print(40, OCT);  // Prints according to ASCII Table: 144
  Serial.print(40, HEX);  // Prints according to ASCII Table: 64
}
void loop()
{

}

This parameter can also be used for floating point numbers, (float, double) this determines the number of decimal places.

void setup()
{
  Serial.Begin(9600);
  
  Serial.print(29.2236, 0);  // print : 29
  Serial.print(29.2236, 1);  // print: 29.2
  Serial.print(29.2236, 4);  // print: 29.2236
}
void loop()
{

}

Serial.println ()

It works similarly. like Serial.print (), the difference at the end of the printed line is the carriage return character ‘\ r’ and the new line character ‘\ n’. So the next print queue starts on a new line. all other parameters are the same as the Serial.print () function.

void setup()
{
  Serial.Begin(9600);
  
  Serial.println("Hello World");
}
void loop()
{

}

The following table shows the pins of the serial port (s) that can be used on Arduino tables.

TABLERX pinsTX pins
Uno, Nano, Mini01
mega0, 15, 17, 191, 14, 16, 18
Serial port pins for use on Arduino boards

I recommend to practice:

In the next section, we will look at variables and data types.