0% found this document useful (0 votes)
5 views

Unit 2- Standard Library Functions (1)

The document provides detailed notes on Arduino programming, focusing on the configuration and use of pins as inputs and outputs, including the use of pull-up resistors. It explains the pinMode() and digitalWrite() functions, as well as the analogRead() function for reading voltage values from analog pins. Additionally, it covers the usage of the Arduino Math library for mathematical operations and functions.

Uploaded by

Nivedan V ECS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit 2- Standard Library Functions (1)

The document provides detailed notes on Arduino programming, focusing on the configuration and use of pins as inputs and outputs, including the use of pull-up resistors. It explains the pinMode() and digitalWrite() functions, as well as the analogRead() function for reading voltage values from analog pins. Additionally, it covers the usage of the Arduino Math library for mathematical operations and functions.

Uploaded by

Nivedan V ECS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SRCAS-ECS

22EC603- Arduino Porgramming Unit 1 Notes

Unit 2- Standard Library

The pins on the Arduino board can be configured as either inputs or outputs. We will explain the
functioning of the pins in those modes. It is important to note that a majority of Arduino analog
pins, may be configured, and used, in exactly the same manner as digital pins.

Pins Configured as INPUT

Arduino pins are by default configured as inputs, so they do not need to be explicitly declared as
inputs with pinMode() when you are using them as inputs. Pins configured this way are said to
be in a high-impedance state. Input pins make extremely small demands on the circuit that they
are sampling, equivalent to a series resistor of 100 megaohm in front of the pin.

This means that it takes very little current to switch the input pin from one state to another. This
makes the pins useful for such tasks as implementing a capacitive touch sensor or reading an
LED as a photodiode.

Pins configured as pinMode(pin, INPUT) with nothing connected to them, or with wires
connected to them that are not connected to other circuits, report seemingly random changes in
pin state, picking up electrical noise from the environment, or capacitively coupling the state of a
nearby pin.Pull-up Resistors

Pull-up resistors are often useful to steer an input pin to a known state if no input is present. This
can be done by adding a pull-up resistor (to +5V), or a pull-down resistor (resistor to ground) on
the input. A 10K resistor is a good value for a pull-up or pull-down resistor.

Using Built-in Pull-up Resistor with Pins Configured as Input

There are 20,000 pull-up resistors built into the Atmega chip that can be accessed from software.
These built-in pull-up resistors are accessed by setting the pinMode() as INPUT_PULLUP. This
effectively inverts the behavior of the INPUT mode, where HIGH means the sensor is OFF and
LOW means the sensor is ON. The value of this pull-up depends on the microcontroller used. On
most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the Arduino
Due, it is between 50kΩ and 150kΩ. For the exact value, consult the datasheet of the
microcontroller on your board.

When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should be
connected to the ground. In case of a simple switch, this causes the pin to read HIGH when the
switch is open and LOW when the switch is pressed. The pull-up resistors provide enough
current to light an LED dimly connected to a pin configured as an input. If LEDs in a project
seem to be working, but very dimly, this is likely what is going on.

Same registers (internal chip memory locations) that control whether a pin is HIGH or LOW
control the pull-up resistors. Consequently, a pin that is configured to have pull-up resistors
turned on when the pin is in INPUTmode, will have the pin configured as HIGH if the pin is then
switched to an OUTPUT mode with pinMode(). This works in the other direction as well, and an

Prepared by Mr V Nivedan ECS


SRCAS-ECS
22EC603- Arduino Porgramming Unit 1 Notes

output pin that is left in a HIGH state will have the pull-up resistor set if switched to an input
with pinMode().

pinMode(3,INPUT) ; // set pin to input without using built in pull up resistor

pinMode(5,INPUT_PULLUP) ; // set pin to input using built in pull up

resistor

Pins Configured as OUTPUT

Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means
that they can provide a substantial amount of current to other circuits. Atmega pins can source
(provide positive current) or sink (provide negative current) up to 40 mA (milliamps) of current
to other devices/circuits. This is enough current to brightly light up an LED (do not forget the
series resistor), or run many sensors but not enough current to run relays, solenoids, or motors.

Attempting to run high current devices from the output pins, can damage or destroy the output
transistors in the pin, or damage the entire Atmega chip. Often, this results in a "dead" pin in the
microcontroller but the remaining chips still function adequately. For this reason, it is a good
idea to connect the OUTPUT pins to other devices through 470Ω or 1k resistors, unless
maximum current drawn from the pins is required for a particular application.

pinMode() Function

The pinMode() function is used to configure a specific pin to behave either as an input or an
output. It is possible to enable the internal pull-up resistors with the mode INPUT_PULLUP.
Additionally, the INPUT mode explicitly disables the internal pull-ups.

pinMode() Function Syntax

Void setup () {

pinMode (pin , mode);


}
∙ pin − the number of the pin whose mode you wish to set

∙ mode − INPUT, OUTPUT, or INPUT_PULLUP.

Example

int button = 5 ; // button connected to pin 5


int LED = 6; // LED connected to pin 6

void setup () {
pinMode(button , INPUT_PULLUP);
// set the digital pin as input with pull-up resistor
pinMode(button , OUTPUT); // set the digital pin as output
Prepared by Mr V Nivedan ECS
SRCAS-ECS
22EC603- Arduino Porgramming Unit 1 Notes

void setup () {
If (digitalRead(button ) == LOW) // if button pressed {
digitalWrite(LED,HIGH); // turn on led
delay(500); // delay for 500 ms
digitalWrite(LED,LOW); // turn off led
delay(500); // delay for 500 ms
}
}

digitalWrite() Function

The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin. If the pin
has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding
value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as
an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input
pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up
resistor.

If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling
digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(),
digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current
limiting resistor.

digitalWrite() Function Syntax


Void loop() {
digitalWrite (pin ,value);
}
∙ pin − the number of the pin whose mode you wish to set

∙ value − HIGH, or LOW.

Example

int LED = 6; // LED connected to pin 6

void setup () {
pinMode(LED, OUTPUT); // set the digital pin as output
}

void setup () {
Prepared by Mr V Nivedan ECS
SRCAS-ECS
22EC603- Arduino Porgramming Unit 1 Notes

digitalWrite(LED,HIGH); // turn on led


delay(500); // delay for 500 ms
digitalWrite(LED,LOW); // turn off led
delay(500); // delay for 500 ms
}

analogRead( ) function

Arduino is able to detect whether there is a voltage applied to one of its pins and report it through
the digitalRead() function. There is a difference between an on/off sensor (which detects the
presence of an object) and an analog sensor, whose value continuously changes. In order to read
this type of sensor, we need a different type of pin.

In the lower-right part of the Arduino board, you will see six pins marked “Analog In”. These
special pins not only tell whether there is a voltage applied to them, but also its value. By using
the analogRead() function, we can read the voltage applied to one of the pins.

This function returns a number between 0 and 1023, which represents voltages between 0 and 5
volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns
512.

analogRead() function Syntax

analogRead(pin);
∙ pin− the number of the analog input pin to read from (0 to 5 on most
boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)

Example

int analogPin = 3;//potentiometer wiper (middle terminal)


// connected to analog pin 3
int val = 0; // variable to store the value read

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

void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}

In this chapter, we will learn some advanced Input and Output Functions.
Prepared by Mr V Nivedan ECS
SRCAS-ECS
22EC603- Arduino Porgramming Unit 1 Notes

analogReference() Function

Configures the reference voltage used for analog input (i.e. the value used
as the top of the input range). The options are −

∙ DEFAULT − The default analog reference of 5 volts (on 5V Arduino


boards) or 3.3 volts (on 3.3V Arduino boards)
∙ INTERNAL − An built-in reference, equal to 1.1 volts on the ATmega168
or ATmega328 and 2.56 volts on the ATmega8 (not available on the
Arduino Mega) ∙ INTERNAL1V1 − A built-in 1.1V reference (Arduino Mega
only)
∙ INTERNAL2V56 − A built-in 2.56V reference (Arduino Mega only)

∙ EXTERNAL − The voltage applied to the AREF pin (0 to 5V only) is used


as the reference

analogReference() Function Syntax


analogReference (type);

type − can use any type of the follow (DEFAULT, INTERNAL,


INTERNAL1V1, INTERNAL2V56, EXTERNAL)

Do not use anything less than 0V or more than 5V for external reference voltage on the AREF
pin. If you are using an external reference on the AREF pin, you must set the analog reference to
EXTERNAL before calling the analogRead() function. Otherwise, you will short the active
reference voltage (internally generated) and the AREF pin, possibly damaging the
microcontroller on your Arduino board.

Alternatively, you can connect the external reference voltage to the AREF pin through a 5K
resistor, allowing you to switch between external and internal reference voltages.

Note that the resistor will alter the voltage that is used as the reference because there is an
internal 32K resistor on the AREF pin. The two act as a voltage divider. For example, 2.5V
applied through the resistor will yield 2.5 * 32 / (32 + 5) = ~2.2V at the AREF pin.

Prepared by Mr V Nivedan ECS


SRCAS-ECS
22EC603- Arduino Porgramming Unit 1 Notes

Example

int analogPin = 3;// potentiometer wiper (middle terminal) connected to analog pin 3
int val = 0; // variable to store the read value

void setup() {
Serial.begin(9600); // setup serial
analogReference(EXTERNAL); // the voltage applied to the AREF pin (0 to 5V only)
// is used as the reference.
}

void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}

The Arduino Math library (math.h) includes a number of useful mathematical functions for
manipulating floating-point numbers.

Library Macros

Following are the macros defined in the header math.h −

Given below is the list of macros defined in the header math.h

Library Functions

The following functions are defined in the header math.h −

Given below is the list of functions are defined in the header math.h

Explore our latest online courses and learn new skills at your own pace. Enroll and become a
certified expert to boost your career.

Example

The following example shows how to use the most common math.h library
functions −
double double__x = 45.45 ;
double double__y = 30.20 ;

Prepared by Mr V Nivedan ECS


SRCAS-ECS
22EC603- Arduino Porgramming Unit 1 Notes
void setup() {
Serial.begin(9600);
Serial.print("cos num = ");
Serial.println (cos (double__x) ); // returns cosine of x
Serial.print("absolute value of num = ");
Serial.println (fabs (double__x) ); // absolute value of a float
Serial.print("floating point modulo = ");
Serial.println (fmod (double__x, double__y)); // floating point modulo
Serial.print("sine of num = ");
Serial.println (sin (double__x) ) ;// returns sine of x
Serial.print("square root of num : ");
Serial.println ( sqrt (double__x) );// returns square root of x
Serial.print("tangent of num : ");
Serial.println ( tan (double__x) ); // returns tangent of x
Serial.print("exponential value of num : ");
Serial.println ( exp (double__x) ); // function returns the exponential value of
x. Serial.print("cos num : ");

Serial.println (atan (double__x) ); // arc tangent of x


Serial.print("tangent of num : ");
Serial.println (atan2 (double__y, double__x) );// arc tangent of y/x
Serial.print("arc tangent of num : ");
Serial.println (log (double__x) ) ; // natural logarithm of x
Serial.print("cos num : ");
Serial.println ( log10 (double__x)); // logarithm of x to base 10.
Serial.print("logarithm of num to base 10 : ");
Serial.println (pow (double__x, double__y) );// x to power of y
Serial.print("power of num : ");
Serial.println (square (double__x)); // square of x
}

void loop() {

Result
cos num = 0.10
absolute value of num =
45.45 floating point modulo
=15.25 sine of num = 0.99
square root of num :
6.74 tangent of num :
9.67
exponential value of num : ovf
cos num : 1.55
tangent of num : 0.59
arc tangent of num : 3.82

Prepared by Mr V Nivedan ECS


SRCAS-ECS
22EC603- Arduino Porgramming Unit 1 Notes

cos num : 1.66


logarithm of num to base 10 : inf
power of num : 2065.70
Print Page

Trigonometric Exact Syntax

double sin(double x); //returns sine of x radians


double cos(double y); //returns cosine of y radians
double tan(double x); //returns the tangent of x radians
double acos(double x); //returns A, the angle corresponding to cos (A) = x
double asin(double x); //returns A, the angle corresponding to sin (A) = x
double atan(double x); //returns A, the angle corresponding to tan (A) = x
Example
double sine = sin(2); // approximately 0.90929737091
double cosine = cos(2); // approximately -0.41614685058
double tangent = tan(2); // approximately -2.18503975868

Print Page
Prepared by Mr V Nivedan ECS

You might also like