0% found this document useful (0 votes)
4 views4 pages

Basic Arduino Functions Guide

This document serves as a quick reference guide for beginners using Arduino, detailing essential functions such as setup(), loop(), pinMode(), digitalWrite(), and more. It includes examples and explanations for configuring pins, reading inputs, and utilizing serial communication. Additionally, it provides important notes and best practices for effective programming with Arduino.

Uploaded by

vsruthi25209
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)
4 views4 pages

Basic Arduino Functions Guide

This document serves as a quick reference guide for beginners using Arduino, detailing essential functions such as setup(), loop(), pinMode(), digitalWrite(), and more. It includes examples and explanations for configuring pins, reading inputs, and utilizing serial communication. Additionally, it provides important notes and best practices for effective programming with Arduino.

Uploaded by

vsruthi25209
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

Basic Arduino Functions

Quick Reference Guide for Beginners

This document introduces the most commonly used Arduino functions for beginners. Examples are written in
Arduino C/C++ style and can be used with boards such as the Arduino Uno.

1. Arduino Program Structure


Every Arduino sketch normally contains setup() and loop(). setup() runs once when the board starts or resets.
loop() runs repeatedly.

void setup() {
// Initialization code
}

void loop() {
// Code that repeats
}

2. pinMode()
Used to configure a digital pin as INPUT, OUTPUT, or INPUT_PULLUP.

pinMode(pin, mode);

// Example
pinMode(13, OUTPUT);
pinMode(2, INPUT);
pinMode(4, INPUT_PULLUP);

3. digitalWrite()
Sets a digital output pin HIGH (approximately 5 V on a typical Uno) or LOW (0 V).

digitalWrite(pin, value);

// Example
digitalWrite(13, HIGH);
digitalWrite(13, LOW);

4. digitalRead()
Reads the logic level of a digital input pin. It returns HIGH or LOW.

int state = digitalRead(2);

if (state == HIGH) {
// Input is HIGH
}
5. analogRead()
Reads an analog input. On a typical Arduino Uno, analogRead() returns a value from 0 to 1023 for an input voltage
from 0 V to the reference voltage (normally about 5 V).

int value = analogRead(A0);


[Link](value);

6. analogWrite()
Outputs a PWM signal on supported PWM pins. The value is normally 0 to 255: 0 = 0% duty cycle, 255 = 100%
duty cycle. It is commonly used for LED brightness or motor control.

analogWrite(9, 128); // approximately 50% duty cycle


analogWrite(9, 255); // 100%
analogWrite(9, 0); // 0%

7. delay()
Pauses the program for the specified number of milliseconds.

delay(1000); // 1 second
delay(500); // 0.5 second

Note: delay() blocks the processor from executing the next statements during the delay.

8. millis()
Returns the number of milliseconds since the Arduino program started. It is useful for timing without blocking the
program with delay().

unsigned long startTime = millis();

9. micros()
Returns the number of microseconds since the Arduino program started. It is useful for short timing measurements.

unsigned long t = micros();

10. Serial Communication


Serial functions are useful for debugging and communicating with a computer or another serial device.

void setup() {
[Link](9600);
}

void loop() {
[Link]("Hello Arduino");
delay(1000);
}

Common Serial functions:

 [Link](baudrate) – starts serial communication.


 [Link]() – prints data without moving to a new line.
 [Link]() – prints data and moves to a new line.
 [Link]() – reads incoming serial data.
 [Link]() – checks whether received data is available.

11. Basic Data Types


Type Example Use
int int count = 10; Integer values
float float temp = 25.5; Decimal values
char char c = 'A'; Single character
bool bool flag = true; true/false
unsigned long unsigned long t = millis(); Timing values

12. Constants Commonly Used


HIGH // Logic HIGH
LOW // Logic LOW
INPUT // Digital input
OUTPUT // Digital output
INPUT_PULLUP // Input with internal pull-up resistor

13. A Simple LED Blink Example


This example demonstrates pinMode(), digitalWrite(), and delay().

const int LED_PIN = 13;

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

void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);

digitalWrite(LED_PIN, LOW);
delay(1000);
}

14. Button + LED Example


The LED turns ON when the button is pressed. INPUT_PULLUP is used, so the input reads LOW when the button
is pressed.

const int LED_PIN = 13;


const int BUTTON_PIN = 2;

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}

15. Analog Sensor Example


Reads a potentiometer connected to A0 and displays the raw ADC value on the Serial Monitor.

const int SENSOR_PIN = A0;

void setup() {
[Link](9600);
}

void loop() {
int sensorValue = analogRead(SENSOR_PIN);
[Link](sensorValue);
delay(200);
}

16. Quick Function Reference


Function Purpose Example
setup() Runs once at startup void setup() {}
loop() Runs repeatedly void loop() {}
pinMode() Configures a pin pinMode(13, OUTPUT);
digitalWrite() Sets digital output digitalWrite(13, HIGH);
digitalRead() Reads digital input digitalRead(2);
analogRead() Reads analog input analogRead(A0);
analogWrite() Generates PWM analogWrite(9, 128);
delay() Blocking delay in ms delay(1000);
millis() Elapsed time in ms millis();
[Link]() Starts serial [Link](9600);
[Link]() Prints a line [Link](value);

17. Important Notes for Students


 Always configure a digital pin with pinMode() before using it as an input or output.
 Do not connect an LED directly to a GPIO pin without an appropriate current-limiting resistor.
 Check the board's pinout before using PWM, analog, or special-function pins.
 analogWrite() is PWM output on boards such as the Uno; it is not a true analog voltage output.
 Use millis() when you need multiple tasks to run without blocking the program with delay().
 Use Serial Monitor frequently to debug sensor readings and program flow.

You might also like