Arduino - introduction



Arduino is a small circuit board that you can use to bring the physical world into the data world, and the data world into the physical world. For example, you can measure temperature, light, CO2, distance, movement or humidity, and connect buttons, switches or displays to control and display information, or motors to set something in motion. If you see an electronic installation, it is likely that Arduino is in use.





Blink built-in LED



void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}


Fade LED




int ledPin = 9;        // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 10;   // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode( ledPin, OUTPUT );

  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9:
  analogWrite(ledPin, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
  Serial.println( brightness );

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}


Potentiometer




int potPin = A2;    // select the ANALOG input pin for the potentiometer
int value = 0;       // variable to store the value coming from the sensor

void setup() {
  Serial.begin( 9600 ); // Start Serial
  pinMode( potPin, INPUT );  // declare the ledPin as an OUTPUT
}

void loop() {
  value = analogRead( potPin );    // read the value from the sensor
  Serial.println( value ); // Write to Serial
}


Potentiometer controlling LED brightness



int potPin = A2;    // select the ANALOG input pin for the potentiometer
int val = 0;       // variable to store the value coming from the sensor
int ledPin = 9;
int brightness = 255;

void setup() {
  Serial.begin( 9600 ); // Start Serial
  pinMode( potPin, INPUT );  // declare the ledPin as an OUTPUT
  pinMode( ledPin, OUTPUT );
}

void loop() {
  val = analogRead( potPin );    // read the value from the sensor
  Serial.print( "Potentiometer: " );
  Serial.println( val ); // Write to Serial

  brightness = map( val, 0, 1023, 0, 255 ); // Scale analog potentiometer values to LED values
  Serial.print( "Brightness: " );
  Serial.println( brightness );
  analogWrite( ledPin, brightness );
}