Arduino Programming Cheat Sheet




Arduino Cheatsheet very handy! The Mechatronics Guy writes –

C Programming For Beginners Master The C Language C Programming C Cheatsheet Inc C2011 Cheat Sh In 2020 Cheat Sheets Arduino Programming Computer Programming. A range of boards are available, all programmable using the Arduino IDE and Arduino coding library. This cheat sheet should help you with some of the basic commands you'll need to start programming Arduino boards. FREE DOWNLOAD: This cheat sheet is available as a downloadable PDF from our distribution partner, TradePub.

I really love cheat sheets. In a lot of cases they can take the place of an entire manual. So I was surprised, given its popularity that I couldn’t find a single-page reference for the arduino online. I tried to make a sheet that captured all the things I hit the reference for while programming. What data type does the millis() function return? How long till that overflows again? How large can a long get? What baud rates can the serial handle?

Grab the PDF here.

Arduino Timer / PWM cheat sheet

Arduino Timer

Arduino UNO (ATmega328p) has three 8bit timers

Timer0 - used for millis() micros() delay()… and is on pin 5, 6
Timer1 - 16bit timer is on pin 9, 10
Timer2 - 8bit timer is on pin 3, 11

Arduino Mega has six 8bit timers

Timer0 - millis, micros… and is on pin 4, 13
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.

Dailybook for mac. 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

Arduino Reference Sheet

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.

Sheet

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 5 and 6: 976Hz
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).

Arduino Programming Cheat Sheet

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 HIGH
If value > counter, output LOW

In Phase-correct PWM mode

Browser with vpn for mac. In Fast PWM mode

Arduino Mega Programming Cheat Sheet

Posted by ceez at 14:45:41