You are on page 1of 3

Arduino Cheat Sheet 

Arduino environment uses C not C++, so no classes, but functions, struct etc are available. 

Basics 1: Using Digital I/O, Analog reading, Serial as Output, Basic Timer 

int t=50;  //variables declared as Global, since setup, loop are functions 
int x; 
long time; 
boolean ps13=HIGH; 
void setup()    //run only once at the very start 

  pinMode(13,OUTPUT);  //Defining what a pin does, reqd for digital operations 
  digitalWrite(13,ps13);  //Set value of a pin 
  Serial.begin(9600);    //Start Serial Communication thru USB/ other device 

void loop()        //run over & over again 

  x=digitalRead(9);    //Finding the digital state of the pin 
  if(x>0) 
  { 
  ps13=~ps13; 
  digitalWrite(13,OUTPUT); 
  x=analogRead(A0);  //Using the ADC to find the analog value of a pin 
        //Analog readings from 0 to 1023 
  Serial.println(x);  //Sending data using Serial, also see write/print 
  time=millis();   //millis calculates time from start of program 
  Serial.println(time); 
  delay(500);    //stop everything for x milisecs 
  } 

 
Basics 2: Serial as Input, PWM output 

int x; 
void setup()      //run only once at the very start 

  pinMode(A5,OUTPUT);  //The Analog pins can be used as output too. 
  Serial.begin(9600); 

void loop()      //run over & over again 

  if(Serial.available()>0)  //checks the number of bytes in the buffer 
  { 
  x=Serial.read();    //get the value from Serial 
  analogWrite(9,x);    //using PWM (values 0 to 255) fixed pins support PWM 
  if(x>127) digitalWrite(A5,HIGH); 
  else digitalWrite(A5,LOW); 
  } 

 
Notes: 

 Serial uses a 128 byte buffer in RAM which stores all the values from Serial until a read() is 
called. Calling read() removes the data from the buffer. If more than 128 bytes are sent 
before the buffer is read, some data is lost. The function flush() clears the whole buffer. 
 Time can also be measured in microseconds, using the appropriate functions. 
 The ADC is one of the slowest devices in the ATMEGA328, thus reading an analog value takes 
time, & when you use analogRead(), the uC waits until the reading is complete. Though 
there are many analog input pins, only one ADC. 
 

EEPROM: This is permanent memory which is not lost after power off. A mini HDD. Holds 512 bytes. 
#include <EEPROM.h>    //need to use the EEPROM library 
 
int i; 
char x; 
void setup() 

  Serial.begin(9600); 
  delay(1000); 
  for(i=0;i<5;i++) 
  {  x=EEPROM.read(i);  //Reading the value stored at "i" in EEPROM 
    Serial.println(x); 
  } 
  i=0; 

void loop() 
{  if(i<5) 
  {  if(Serial.available()>0) 
    { 
    x=Serial.read(); 
    EEPROM.write(i,x);  //Writing value x to "i"th address in EEPROM 
    Serial.println(EEPROM.read(i)); 
    i++; 
    } 


 
Interrupts: 

Interrupt the normal functioning when something happens. Thus you can run the main program, & 
interrupts will take care of the other stuff eg. obstacles in the path. Arduino (atmega328) has 2 
external interrupts 0(pin 2) & 1(pin3). 
 
volatile int count;  //variables changed in interrupts are specified as volatile 
int starttime; 
void setup() 

  Serial.begin(9600); 
  attachInterrupt(0,myfunction,RISING);  //set ext interrupt0 for RISE in input 
  starttime=millis(); 

void loop() 

  if(millis()‐starttime>1000) 
  {   
    noInterrupts();    //disables ALL interrupts 
    Serial.println(count/10000); 
    count=0; 
    interrupts();    //enable ALL interrupts 
    starttime=millis(); 
  } 

void myfunction()    //function called when ext interrupt event occurs 
{  count++;  }  //must have no parameters or return values! 
 
When an interrupt function is running, all other timer based activities do not function. Many routine 
operations would not work when interrupt function runs eg. Serial Input Communication, Timers. So 
make the Interrupt functions short. 
Interrupt functions are of type void & have no parameters. 
External interrupts can be called for the modes LOW, RISING, CHANGE, FALLING. 
Only External interrupt can be disabled by detachInterrupt(interrupt number) 
 
 

 
The Arduino‐ Hardw
ware 

Its heartt is an 8 bit A
AVR microcontroller ATMMEGA328 runnning at 16 MMHz. There iss another 
FTDI/ATTMEGA 8 chip p on the Arduino to proggram the main uC. In the Arduino, thee 328 is load
ded with 
its own bootloader, which makes it possible  to program thru USB.

328 has many devicees inside it like: 

 CD) are used for I/O. 
4 Ports (ABC
 ADC used foor Analog to Digital conveersion 
 Timers for ru
unning vario
ous devices && PWM. 
 USART for Serial Commu unication. 
 AND lots of other stuff! 

1. Many pins share multiple features egg. ADC with PortC, Interrrupts/PWM TTimers with other 
Ports. 
2. All devices h have control registers whhich decide h
how that devvice runs. Maany more reggisters 
inside the CP PU too. 
3. Some devicees have interrrupts too. T hey can be u used by creatting your ow
wn functions. 
4. The Arduino o can also be coded usingg standard AVR Code as wwell. 
Do havee a look at: 

1. www.atmel..com/dyn/re esources/prood_documen
nts/doc8271.pdf 
2. www.arduin no.cc/ 
3. arduino.cc/een/Tutorial/H
HomePage 

You might also like