Select Page

1.        Basic

1.1.       Setup and Loop

viod setup(){}

viod loop(){}

 

1.2.       Comment

//single line comment

/*multi-line comment*/

 

1.3.       Delay

Unit is ms

delay(1000);

 

2.        Hardware

2.1.       Buildin LED

Instead of using “int led = 13”, use:

LED_BUILTIN

 

2.2.       Serial Monitor

void setup() {

Serial.begin(9600);

}

void loop() {

Serial.println(buttonState);

delay(1);

}

 

3.        Mathematics

3.1.       Variable as Integer

-32768 to 32768, step of 1 (whole number)

int pushButton = 2;

or

int pushButton;

pushButton = 2;

 

3.2.       Variable as Float

With decimal point. Total 7 digit. Very slow.

float voltage;

 

3.3.       Constant Variable

If the variable is fixed

const int led = 13;

 

3.4.       Array

int example[]={1,3,5,7};

example[3];

 

3.5.       Mapping

change X scale to Y scale

map(X,minX,maxX,minY,maxY);

 

3.6.       Limit Value

constrain(value,lowerLimit,upperLimit);

 

3.7.       Other Mathematics Function

abs();

min(a,b);

max(a,b);

pow(base,exponent) // 2^4 = pwo(2,4);

sq();

sqrt();

cos();   //in radian

sin();  //in radian

tan();  //in radian

random(min,max);

 

4.        Control

4.1.       If or If-Else

void loop() {

int analogValue = analogRead(analogPin);

 

if (analogValue > threshold || analogValue ==44) {

digitalWrite(ledPin, HIGH);

} else {

digitalWrite(ledPin, LOW);

}

 

Serial.println(analogValue);

delay(1);

}

 

4.2.       For Loop

void setup() {

for (int thisPin = 2; thisPin < 8; thisPin++) {

pinMode(thisPin, OUTPUT);

}

}

void loop() {

for (int thisPin = 2; thisPin < 8; thisPin++) {

digitalWrite(thisPin, HIGH);

delay(100);

digitalWrite(thisPin, LOW);

}

}

5.        Input Output

 

5.1.       Digital In

int pushButton = 2;

void setup() {

Serial.begin(9600);

pinMode(pushButton, INPUT);

}

void loop() {

int buttonState = digitalRead(pushButton);

Serial.println(buttonState);

delay(1);

}

 

5.2.       Digital Out

int led = 13;

viod setup(){

pinMode(led,OUTPUT);

}

viod loop(){

digitalWrite(led,HIGH);

delay(1000);

digitalWrite(led,LOW);

delay(1000);

}

 

5.3.       Analog In

Integer value is from 0 to 1023, cover 0V to 5V.

void setup() {

Serial.begin(9600);

}

void loop() {

int sensorValue = analogRead(A0);

float voltage = sensorValue/1023*5;

Serial.println(voltage);

delay(1);

}

 

5.4.       Analog Out

Use the digital port that have PWM marking.

Duty cycle is from 0 to 255.

Frequency at 490 Hz.

int led = 9;

int dutyCycle = 100;

 

void setup() {

pinMode(led, OUTPUT);

}

 

void loop() {

analogWrite(led, dutyCycle);

}

 

5.5.       PWM with Different Frequency

PWM.h library needs to be included.

Refer to [3] and [4] to download and install library.

#include <PWM.h>

int led = 9;                // the pin that the LED is attached to

int32_t frequency = 40000;  // frequency (in Hz)

int dutyCycle = 150;        // range from 0 to 255

void setup()

{

InitTimersSafe();

bool success = SetPinFrequencySafe(led, frequency);

}

void loop()

{

pwmWrite(led,dutyCycle);

}

6.        References

  1. Arduino Course for Absolute Beginners, https://www.youtube.com/watch?v=09zfRaLEasY&list=PLZfay8jtbyJt6gkkOgeeapCS_UrsgfuJA
  2. Arduino Basics Change your PWM Frequency, https://www.youtube.com/watch?v=gMB88fXOZ-g
  3. Library for PWM, https://code.google.com/archive/p/arduino-pwm-frequency-library/downloads
  4. Guide to Install New Library, https://www.arduino.cc/en/Guide/Libraries
  5. Math Functions in Arduino Part 1, https://www.youtube.com/watch?v=CVRk3XO_6MQ