0% found this document useful (0 votes)
23 views3 pages

Arduino Function Cheat Sheet

Uploaded by

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

Arduino Function Cheat Sheet

Uploaded by

nahidniaz123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arduino Function Cheat Sheet

Basic Functions

setup() - Runs once at startup

loop() - Runs repeatedly after setup()

delay(ms) - Pause program for milliseconds

millis() - Time since program start in ms

micros() - Time since program start in µs

delayMicroseconds(us) - Pause for microseconds

Pin Functions

pinMode(pin, mode) - Set pin as INPUT, OUTPUT, or INPUT_PULLUP

digitalWrite(pin, val) - Write HIGH or LOW

digitalRead(pin) - Read HIGH or LOW

analogRead(pin) - Read analog value (0-1023)

analogWrite(pin, val) - Write PWM (0-255)

Serial Communication

[Link](baud) - Begin serial communication

[Link](val) - Print without newline

[Link](val) - Print with newline

[Link]() - Read byte from serial

[Link]() - Check data availability

[Link]() - Read incoming string

Math & Utility

min(a, b) - Smaller of two values


Arduino Function Cheat Sheet

max(a, b) - Larger of two values

abs(x) - Absolute value

constrain(x, a, b) - Keep x between a and b

map(x, in1, in2, out1, out2) - Map range

pow(base, exp) - Raise to power

sqrt(x) - Square root

random(min, max) - Random in range

randomSeed(seed) - Seed random generator

Bit & Byte Functions

bitRead(x, n) - Read bit n of x

bitWrite(x, n, b) - Write bit to n of x

bitSet(x, n) - Set bit n

bitClear(x, n) - Clear bit n

highByte(x) - High byte of word

lowByte(x) - Low byte of word

Timing & Interrupts

attachInterrupt() - Setup hardware interrupt

detachInterrupt() - Remove interrupt

pulseIn(pin, val) - Pulse duration

Control Structures

if, else, switch - Conditional statements


Arduino Function Cheat Sheet

for, while, do - Loops

break, continue - Loop control

return - Exit function

Common questions

Powered by AI

Serial.begin() initializes serial communication at a specified baud rate, which is the speed of communication measured in bits per second . It sets up the Arduino to send or receive data with another device, such as a computer. This function is essential for debugging, logging data, or controlling Arduino from a computer or other hardware. A practical application would include using Serial.begin(9600) to establish communication with a computer for monitoring sensor data via a serial monitor, providing real-time feedback and aiding in troubleshooting and development processes.

randomSeed() initializes the random number generator with a seed, which is crucial for generating different sequences of pseudorandom numbers each time a program runs, enhancing unpredictability and variability . This function is vital when unique random sequences are required for applications like gaming algorithms or randomized testing, preventing predictable patterns. Omitting randomSeed() leads to generating the same sequence on subsequent runs if the default seed (0) is used, resulting in repetitiveness and potential predictability in applications requiring dynamic responses. Ensuring a unique seed, possibly using analog readings for variability, widens the range of possible outputs, enriching the application experience.

The map() function is beneficial in adjusting input values from one range to a new range, making it particularly useful when dealing with analog inputs that need to be standardized or converted to a different scale for further processing . For example, reading an analog input from a temperature sensor might yield values ranging from 0 to 1023, which could then be mapped to a temperature range in degrees Celsius. This function is invaluable when uniform scaling is necessary, such as translating sensor data into meaningful units within specific application constraints. It allows the programmer to seamlessly integrate and adapt different hardware components without manual scaling.

if, else, and switch statements facilitate decision-making by allowing the program to execute different code blocks based on conditions . An if statement evaluates a condition and executes code if the condition is true, while else specifies what to do if it is false. switch provides an efficient mechanism to handle multiple possible values of a variable, often replacing complex if-else chains with cleaner code when dealing with discrete cases. However, their limitations include increased complexity in scenarios with deeply nested conditions, potential for increased code size, and difficulty in maintaining readability when logic becomes excessively intricate. Choosing appropriately between these structures depends on the logic and branching complexity necessary for the application.

pulseIn() measures the duration of a pulse on a specified pin, returning the length in microseconds for either a HIGH or LOW pulse . This is suitable for applications needing precise timing measurements, such as calculating the range of an ultrasonic distance sensor. In contrast, digitalRead() simply provides the instantaneous logic state of a digital pin without regard to time. It is used where the presence or absence of a signal matters rather than its duration, applicable for state-based controls like switches or buttons. These differences highlight pulseIn() as being valuable for timing-critical applications, whereas digitalRead() serves state-driven logic without specific timing requirements.

bitSet() sets a specific bit of a variable to 1, while bitClear() sets it to 0, enabling direct manipulation of binary data at the bit level . This low-level control facilitates efficient handling of data for performance-critical applications or when interfacing with digital circuits requiring precise signal manipulation. For example, configuring hardware registers or managing individual bits of a device's status allows for fine-tuned control over electronic components, such as initializing flags, configuring digital ports, or implementing binary protocols reliably, which is essential for timing-sensitive operations and resource-constrained environments.

Both digitalRead() and analogRead() are used to read values from the pins on an Arduino board, but they serve different purposes. digitalRead() reads the digital value (HIGH or LOW) from a specified digital pin, implying that the input is either high voltage (1) or low voltage (0). On the other hand, analogRead() is used to read the analog value (ranging from 0 to 1023) from a specified analog pin, allowing for more granular input levels, typically understanding the voltage level within a range relevant to the board's reference voltage . Both functions are integral for sensor input, but they process inputs with different data resolutions.

attachInterrupt() is used to configure a function to run when a specific digital pin changes state, which is crucial in scenarios requiring immediate attention to external events, like responding to a button press or receiving a signal from a rotary encoder . If not used in such scenarios where reaction time is critical, the system might miss these events when occupied with other tasks due to its sequential processing nature. This could lead to delayed responses, missed actions, or incorrect system states, impacting the reliability and accuracy of the application, such as in real-time control systems.

The delay() function pauses the execution of the program for a specified number of milliseconds, effectively freezing all operations during this time . Conversely, millis() serves a non-blocking purpose, providing the number of milliseconds since the Arduino board started running the current program. This allows for more sophisticated time-based event management without pausing other operations, thereby facilitating multi-tasking . Using millis() is more suitable for applications requiring continuous operation alongside timing, such as blinking LEDs while continuing to read sensor data.

constrain() is used to keep a variable within a specified range, which prevents values from exceeding predefined limits, ensuring system stability and preventing erroneous behaviors . Not using constrain() in scenarios requiring bounded inputs, such as controlling the speed of a motor or constraining sensor data to acceptable thresholds, may lead to unpredictable system behaviors. For instance, sending inappropriate values to an actuator could result in mechanical stress or logical errors. Using constrain() ensures consistent data handling and system safety, minimizing the risks of operational failure.

You might also like