Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Arduino-Functions-and-Commands-Set-1

The document outlines essential Arduino functions and commands, including setup() and loop() for initializing and controlling programs. It details digital and analog I/O functions such as digitalRead(), digitalWrite(), analogRead(), and analogWrite(), along with their purposes and use cases. Additionally, it covers data types (int, float, String), memory usage, and basic serial communication functions like Serial.begin(), Serial.print(), and Serial.println().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arduino-Functions-and-Commands-Set-1

The document outlines essential Arduino functions and commands, including setup() and loop() for initializing and controlling programs. It details digital and analog I/O functions such as digitalRead(), digitalWrite(), analogRead(), and analogWrite(), along with their purposes and use cases. Additionally, it covers data types (int, float, String), memory usage, and basic serial communication functions like Serial.begin(), Serial.print(), and Serial.println().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Arduino Functions/Commands

Sketch
1. setup() - The setup() function is called when a sketch starts. Use it to initialize
variables, pin modes, start using libraries, etc. The setup() function will only run once,
after each powerup or reset of the Arduino board.
2. loop() - After creating a setup() function, which initializes and sets the initial values,
the loop() function does precisely what its name suggests, and loops consecutively,
allowing your program to change and respond. Use it to actively control the Arduino
board.
Digital I/O vs Analog I/O
Let's explore the functions, purposes, and differences between digitalRead(),
digitalWrite(), analogRead(), and analogWrite() in Arduino programming. These
functions are fundamental for interacting with the digital and analog pins of your Arduino
board.
1. digitalRead(pin) -
 Purpose: Reads the digital value (HIGH or LOW) from a specified digital pin.
 pin: The pin number you want to read from (an integer).
 Return Value: HIGH (typically 5V or 3.3V, depending on your Arduino board) or
LOW (0V).
 Use Cases:
o Detecting the state of a button (pressed or not pressed).
o Reading the output of a digital sensor (e.g., a motion sensor).
o Checking the state of a switch.
2. digitalWrite(pin, value) -
 Purpose: Sets the digital value (HIGH or LOW) of a specified digital pin.
 pin: The pin number you want to write to (an integer).
 value: HIGH or LOW.
 Use Cases:
o Controlling LEDs (turning them on or off).
o Controlling relays.
o Controlling other digital devices.
3. analogRead(pin) -
 Purpose: Reads the analog value from a specified analog input pin. Analog pins
can read a range of voltages.
 pin: The analog input pin number (e.g., A0, A1, etc.).
 Return Value: An integer value between 0 and 1023 (on most Arduino boards).
This value represents the voltage level at the pin.
 Use Cases:
o Reading the output of analog sensors (e.g., potentiometers, light sensors,
temperature sensors).
o Measuring voltage levels.
4. analogWrite(pin, value) -
 Purpose: Writes an analog value (actually a PWM signal) to a specified digital
pin. Not all digital pins support analogWrite(). These pins are usually marked with
a "~" symbol.
 pin: The PWM-capable digital pin number.
 value: An integer value between 0 and 255. This value controls the duty cycle of
the PWM signal. 0 is always off, 255 is always on, and values in between control
the brightness or power level.
 Use Cases:
o Controlling the brightness of LEDs (dimming).
o Controlling the speed of DC motors.
o Generating audio signals.
Function Input/Output Purpose Value Range
digitalRead() Input Read digital value HIGH/LOW
(HIGH/LOW)
digitalWrite() Output Set digital value (HIGH/LOW) HIGH/LOW
analogRead( Input Read analog value 0-1023 (typical)
)
analogWrite() Output Write analog value (PWM) 0-255
Important Notes:
 Pin Modes: Before using digitalRead() or digitalWrite(), you must configure the
pin's mode using pinMode(). For digital input, use INPUT or INPUT_PULLUP. For
digital output, use OUTPUT.
 PWM Pins: Only specific digital pins on your Arduino board support analogWrite()
(PWM). Check your board's documentation.
 Analog vs. Digital: Digital signals are discrete (HIGH or LOW), while analog
signals are continuous (a range of values). analogRead() converts a voltage level
into a digital value that the Arduino can understand. analogWrite() uses PWM to
simulate an analog output.

Time
delay() - Pauses the program for the amount of time (in milliseconds) specified as
parameter. (There are 1000 milliseconds in a second.)
Variables - Data Types
Let's break down the differences and purposes of float, int, and String data types in
Arduino programming.
1. int (Integer) -
 Purpose: Represents whole numbers (numbers without any fractional or decimal
part). Integers can be positive, negative, or zero.
 Range: The range of values an int can hold depends on the specific Arduino
board you're using. On most Arduino boards (like the Uno), an int is 16 bits,
meaning it can store values from -32,768 to 32,767.
 Use Cases:
o Counting (e.g., loop counters, counting sensor readings)
o Storing quantities that are always whole numbers (e.g., number of LEDs,
number of buttons pressed)
o Performing integer arithmetic (addition, subtraction, multiplication, division)
2. float (Floating-Point) -
 Purpose: Represents numbers that can have a fractional or decimal part. Floats
are used when you need more precision than integers can provide.
 Range and Precision: On most Arduino boards, a float is 32 bits, which gives it a
wider range and the ability to represent numbers with decimals. However,
floating-point numbers have limited precision, meaning they can only store a
certain number of digits accurately.
 Use Cases:
o Storing measurements that may have fractional parts (e.g., temperature
readings, sensor readings with decimals)
o Performing calculations that require decimals (e.g., calculating averages,
trigonometry)
o Representing values that are not whole numbers (e.g., pi = 3.14159)
3. String -
 Purpose: Represents a sequence of characters, essentially text. Strings are used
to store and manipulate text-based data.
 Use Cases:
o Storing messages to display on an LCD screen or send over serial
communication
o Working with text input from a keyboard or other devices
o Manipulating text (e.g., comparing strings, concatenating strings)
Key Differences Summarized:
Feature int float String
Representatio Whole numbers Numbers with decimals Text characters
n
Range Limited by 16 Wider range, limited Variable length
bits precision
Precision Exact Limited Exact (for characters)
Use Cases Counting, whole Measurements, Text, messages,
quantities decimals, calculations communication
Important Notes:
 Choosing the Right Type: It's important to choose the most appropriate data type
for your variables. Using int when you need decimals can lead to loss of
information and using float when you only need whole numbers can be less
memory-efficient.
 Memory Usage: int uses less memory than float, and String can use a significant
amount of memory, especially for long strings. Be mindful of memory usage,
especially on Arduino boards with limited memory.
 String vs. char[]: In Arduino, you can also represent text using character arrays
(char[]). Character arrays are more memory-efficient than the String object, but
they require more manual manipulation.
Communication
1. Serial.begin - Initializes serial communication. This function must be called once,
typically in the setup() function of your Arduino sketch.
2. Serial.print - Sends data to the serial port. The data can be of various types
(integers, floats, characters, strings, etc.). Serial.print() sends the data as is, without
adding any extra characters like a newline. The data is displayed in the Serial Monitor
(or any other serial port receiving program) on the same line.
3. Serial.println - Sends data to the serial port, followed by a newline character
(carriage return and line feed on some systems). This moves the cursor to the
beginning of the next line in the Serial Monitor. The data is displayed in the Serial
Monitor, and the cursor moves to the next line.

Sample Arduino Code – Practice using Serial Monitor in TinkerCad

You might also like