Learn Arduino

Quick Guide

Setting up a pin for output


void setup() {
  pinMode(8, OUTPUT);

}

void loop() {
  

}

Setting up a pin for input


void setup() {
  pinMode(8, INPUT);

}

void loop() {
  

}

Turning an LED on


void setup() {
  pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(8, HIGH);
}

Turning an LED off


void setup() {
  pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(8, LOW);
}

LED Blink


void setup() {
  pinMode(8, OUTPUT);
}

void loop() {
  digitalWrite(8, HIGH);
  delay(1000);
  digitalWrite(8, LOW);
  delay(1000);
}

Renaming pins


#define LED 8

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  digitalWrite(LED, HIGH);
}

For Loop


#define LED 8

void setup() {
  pinMode(LED, OUTPUT);
}

void loop() {
  for(int i; i<5; i++){
    digitalWrite(LED, HIGH);
    delay(1000);
    digitalWrite(LED, HIGH);
    delay(1000);
  }
}

Ultrasonic Sensor


#include <SR04.h>
#define trig_pin 13
#define echo_pin 12
SR04 sr04 = SR04(echo_pin,trig_pin);
int distance = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  distance = sr04.Distance();
}

Servo Motor


#include <Servo.h>
Servo myservo;

void setup() {
  myservo.attach(11);
}

void loop() {
  myservo.write(0);
}

Creating a function