Arduino is an open-source electronics platform that has revolutionized the world of DIY electronics. It is a microcontroller board that can be programmed to control various electronic devices and sensors. Arduino is easy to use and has a vast community of users who share their projects and ideas.
The Arduino board consists of a microcontroller, a USB port, and a set of input/output pins. The microcontroller is the brain of the board, and it can be programmed using the Arduino Integrated Development Environment (IDE). The IDE is a software application that allows users to write, compile, and upload code to the board.
Arduino boards come in different shapes and sizes, and they can be used for a wide range of applications. For example, you can use an Arduino board to control a robot, build a weather station, or create an interactive art installation.
Basics
Command
Description
void setup()
This function is called once when the Arduino board is powered on or reset. It is used to initialize variables, pin modes, and other settings.
void loop()
This function is called repeatedly after setup() is executed. It is used to perform the main tasks of the program.
pinMode(pin, mode)
Sets the mode of a pin to either INPUT or OUTPUT.
digitalWrite(pin, value)
Sets the value of a digital pin to either HIGH or LOW.
digitalRead(pin)
Reads the value of a digital pin and returns either HIGH or LOW.
analogRead(pin)
Reads the value of an analog pin and returns a value between 0 and 1023.
analogWrite(pin, value)
Writes an analog value (PWM) to a pin. The value should be between 0 and 255.
Variables
Command
Description
int
A variable type that stores integer values.
float
A variable type that stores floating-point values.
char
A variable type that stores a single character.
boolean
A variable type that stores either true or false.
const
A keyword used to define a constant variable.
volatile
A keyword used to indicate that a variable may change unexpectedly (e.g. due to an interrupt).
Control Structures
Command
Description
if
A conditional statement that executes code if a certain condition is true.
else
An optional statement that executes code if the if condition is false.
else if
An optional statement that allows for multiple conditions to be checked.
while
A loop that executes code while a certain condition is true.
for
A loop that executes code a specific number of times.
switch
A statement that allows for multiple cases to be checked.
break
A keyword used to exit a loop or switch statement.
continue
A keyword used to skip to the next iteration of a loop.
Functions
Command
Description
void functionName()
A function that does not return a value.
int functionName()
A function that returns an integer value.
float functionName()
A function that returns a floating-point value.
char functionName()
A function that returns a single character.
boolean functionName()
A function that returns either true or false.
functionName(parameter1, parameter2)
A function that takes one or more parameters.
Libraries
Command
Description
#include <libraryName.h>
Includes a library in the sketch.
libraryName.functionName()
Calls a function from a library.
Serial Communication
Command
Description
Serial.begin(baudRate)
Initializes serial communication with a specified baud rate.
Serial.print(value)
Prints a value to the serial monitor.
Serial.println(value)
Prints a value to the serial monitor and adds a newline character.
Serial.available()
Returns the number of bytes available to read from the serial buffer.
Serial.read()
Reads a byte from the serial buffer.
Advanced
Command
Description
attachInterrupt(digitalPin, ISR, mode)
Attaches an interrupt to a digital pin.
detachInterrupt(digitalPin)
Detaches an interrupt from a digital pin.
millis()
Returns the number of milliseconds since the Arduino board was powered on or reset.
micros()
Returns the number of microseconds since the Arduino board was powered on or reset.
delay(milliseconds)
Pauses the program for a specified number of milliseconds.
delayMicroseconds(microseconds)
Pauses the program for a specified number of microseconds.
random(min, max)
Generates a random number between min and max.
map(value, fromLow, fromHigh, toLow, toHigh)
Maps a value from one range to another.
bitRead(value, bit)
Reads the value of a specific bit in a byte.
bitWrite(value, bit, bitValue)
Writes a value to a specific bit in a byte.
bitSet(value, bit)
Sets a specific bit in a byte to 1.
bitClear(value, bit)
Sets a specific bit in a byte to 0.
bitToggle(value, bit)
Toggles the value of a specific bit in a byte.
Conclusion
This cheat sheet covers the basics of Arduino programming, including variables, control structures, functions, libraries, serial communication, and advanced topics. With this information, you should be able to write your own Arduino programs and experiment with different sensors and actuators. Happy hacking!