Arduino can help you build robots or electronic devices. But you have a lot to learn about Arduino because it encompasses the worlds of both hardware and software. The following sections contain nuggets of information about using resistors, gathering the tools you’ll need, and system shortcuts to help you on your way to becoming an. Arduino cheat sheet for beginners(it was already there but reposting for new commers). There are cheatsheets for windows shortcuts, vim, useful linux commands. Arduino cheat sheet resetq1. (pwm) di03.ain1.
Arduino Timer / PWM cheat sheet
The Arduino Basics Cheatsheet is a free PDF download providing concise notes for commonly used commands and operations when using the Arduino platform. It also includes some specific information for the Arduino Uno. This quick guide includes the base material that you’ll use time and again. Arduino Cheat Sheet This is a poster-sized cheat sheet for Arduino programmers. It draws primarily from the Arduino Language Reference, including most of the common, basic syntax and a variety of the built-in functions. It is based on a cheat sheet by Gavin Smith and an SVG adaptation by Frederic Dufourg.
Arduino Timer
Arduino UNO (ATmega328p) has three 8bit timers
Timer1 - 16bit timer is on pin 9, 10
Timer2 - 8bit timer is on pin 3, 11
Arduino Mega has six 8bit timers
Timer1 - is on pin 11, 12
Timer2 - is on pin 9, 10
Timer3 - is on pin 2, 3, 5
Timer4 - is on pin 6, 7, 8
Timer5 - is on pin 46, 45, 44
Note: Don’t mess with Timer0 since we need to use millis() or micros() for measuring time
Software timer interrupt service routine
Arduino UNO cannot do complex timer like a computer can. It heavily depends on frequency oscillator, ie 16MHz. Then we have divisor to scale down main clock and then 8bit counter to help with the PWM or Timer.
There are 3 vectors for each timer that we can use to set up 3 ISRs
Setup Overflow vector ISR to use it like a timer in PC
Or we can use COMPARE VECTOR A and B which is compare to value of OCR2A , OCR2B
The only problem in phase-correct PWM (by default), the counter, e.g. TCNT2, goes from 0 to 255 and goes back from 255 to 0 then it overflows. So that each TIMER2_COMPA_vect and TIMER2_COMPB_vect happens twice when it is in Phase-correct PWM (default) but TIMER2_OVF_vect happens only once.
Anyway, we can set the value of counter TCNT2 manually to *mess* up time and thus changing timer or frequency, depends on what you want. But it is kinda messed up. You know, its hard to predict *butterfly*, if you’re watching too much movies about time travelling.
Read datasheet of atmega328 to know how to set bits for TCCR2A, TCCR2B or TIMSK2 and so forth.
Arduino PWM
Arduino UNO (ATmega328p) has six 8bit PWMs (on 3 timers: timer0 - timer2)
Arduino Mega (ATmega2560) has fifteen 8bit PWMs (on six timers: timer0 - timer5)
Above are settings for Phase-corrected PWM (by arduino default).
analogWrite(PWMpin, value);
Default frequencies of analogWrite():
Arduino Pins 9, 10, 11, and 3: 490Hz
Too low for switch mode power supply (buck converter) and generate audible noise when use for motor or brushless fan (audible tone around 490Hz).
One trick is use the commands above (eg: TCCR1B = TCCR1B & B11111000 | B00000011;) after calling analogWrite() to change the default frequency.
You can double the frequency if we switch to fast PWM.
Otherwise, a custom analog output function should be used instead.
The input value (stored in OCRnA or OCRnB for first and second PWM pin) is compared against the value in an 8bit counter (0-255 and wrap around in register TCNTn).
If value > counter, output LOW
In Phase-correct PWM mode
In Fast PWM mode
Posted by ceez at 14:45:41When you first start programming you’ll find that you’ll have a difficult time just remembering what some of the common commands and constructs are. Fortunately, the Arduino comes with some very thorough reference pages that contain precisely this information. It can be found here:
You also find the reference section by going to the Help Menu->Reference in your Arduino Environment. To begin with, you’ll be using a fairly small set of commands. I’ve included the most commonly used ones below.
pinMode(pin, mode)
Arduino Cheat Sheet Pdf
Description
Configures the specified pin to behave either as an input or an output. See the reference page below.
Parameters
pin: the number of the pin whose mode you wish to set. (int)
mode: either INPUT or OUTPUT.
digitalWrite(pin, value)
Description
Ouputs either HIGH or LOW at a specified pin.
Parameters
pin: the pin number
value: HIGH or LOW
digitalRead(pin)
What it does
Reads the value from a specified pin, it will be either HIGH or LOW.
Parameters
pin: the number of the pin you want to read.
Returns
Either HIGH or LOW
int analogRead(pin)
Description
Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the mini), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per A/D unit.
It takes about 100 us (.0001 s) to read an analog input, so the maximum reading rate is about 10000 times a second.
Parameters
int pin
AnalogRead() accepts one integer specifying the number of the pin to read. Values between 0 and 5 are valid on most boards, and between 0 and 7 on the mini.
Note
Analog pins default to inputs and unlike digital ones, do not need to be declared as INPUT nor OUTPUT
analogWrite(pin, value)
Description
Writes an analog value (PWM wave) to a pin. On newer Arduino boards (including the Mini and BT) with the ATmega168 chip, this function works on pins 3, 5, 6, 9, 10, and 11. Older USB and serial Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite, the pin will generate a steady wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin).
Parameters
pin: the pin to write to.
value: the duty cycle: between 0 and 255. A value of 0 generates a constant 0 volts output at the specified pin; a value of 255 generates a constant 5 volts output at the specified pin. For values in between 0 and 255, the pin rapidly alternates between 0 and 5 volts – the higher the value, the more often the pin is high (5 volts). For example, a value of 64 will be 0 volts three-quarters of the time, and 5 volts one quarter of the time; a value of 128 will be at 0 half the time and 255 half the time; and a value of 192 will be 0 volts one quarter of the time and 5 volts three-quarters of the time.
delay(ms)
Description
Pauses your program for the amount of time (in miliseconds) specified as parameter.
Parameters
ms: the number of milliseconds to pause (there are 1000 milliseconds in a second)
Serial.begin(int speed)
Description
Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates – for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.
Parameters
int datarate, in bits per second (baud)
Returns
nothing
Example:
void setup() {
Serial.begin(9600);// opens serial port, sets data rate to 9600 bps
Arduino Commands Cheat Sheet
}
int Serial.available()
Description
Get the number of bytes (characters) available for reading over the serial port.
Parameters
None
Returns
The number of bytes are available to read in the serial buffer, or 0 if none are available. If any data has come in, Serial.available() will be greater than 0. The serial buffer can hold up to 128 bytes.
Example
int incomingByte = 0;// for incoming serial data
void setup() {
Serial.begin(9600);// opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print(“I received: “);
Serial.println(incomingByte, DEC);
}
}
int Serial.read()
Description
Reads incoming serial data.
Parameters
None
Returns
an int, the first byte of inocming serial data available (or -1 if no data is available).
Example
int incomingByte = 0;// for incoming serial data
void setup() {
Serial.begin(9600);// opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print(“I received: “);
Serial.println(incomingByte, DEC);
}
}
Serial.print(data)
Description
Prints data to the serial port.
Parameter
data: all integer types, including char
Syntax
This command can take many forms:
Serial.print(b) with no format specified, prints b as a decimal number in an ASCII string. For example,
int b = 79;
Serial.print(b);
prints the ASCII string “79”.
Serial.print(b, DEC) prints b as a decimal number in an ASCII string. For example,
int b = 79;
Serial.print(b, DEC);
prints the string “79”.