First Arduino lesson: Blinking LEDs

in #arduino7 years ago

Hi there!

This is my first basic tutorial on how to program your Arduino microcontroller

And that's your finished result, hopefully

What you need:

  1. Microcontroller (Arduino, Elegoo,...)
  2. two 330 Ohms resistors
  3. a blue and a red LED
    ...and of course some jumper wires

Ok let's start!

So first of, connect the red and the blue LED to your Arduino.
It's importante to connect them the right way around.
Note that the longer Leg of the LED is the + and the shorter the - pole.
Connect the - to ground and + to the corresponding digital pins red to pin 2 and blue to pin 1.
*Don't forget to use your 330 Ohms resistors in each circute as shown below, to avoid LED heat ups or damage.*
Or just check out the pictures below.

P1240021.JPG

P1240017.JPG

P1240014.JPG

Functions and Commands to learn:

  1. #define (to name a port e.g.: #define blueLED 3) Note that you must not add a ; after a #define
  2. int (to clarify a variable type int means Integer and it can store a value between -32,768 and +32,767 e.g.: int delaytime = 1000;)
  3. pinMode() (to set a pin as an OUTPUT or INPUT e.g.: pinMode(blueLED, OUTPUT);)
  4. digitalWrite() (to turn a signal on (HIGH) or off (LOW) e.g.: digitalWrite(blueLED, HIGH);)
  5. delay() (waits for a time in milliseconds e.g.: delay(delaytime); You can also use variables!)

Next: program your microcontroller

First we have to think about our goal. The LEDs should blink alternating between blue and red.
To do that you have to set up your LEDs as OUTPUT, then you can write your loop. The loop should contain a function that turns on the blue LED, one waiting a delaytime till another turns the blue one off and the red one on; on more delaytime and a function that turns red off and then the loop starts again from top.
Here's my code:

#define blueLED 3 //Defines digital Port 3 as blueLED
#define redLED 2 //Defines digital Port 2 as redLED
int delaytime = 1000; //Sets the delaytime (How long one LED is turned on)
//
void setup() { //Setup only runs once
pinMode(blueLED, OUTPUT); //Defines blueLED as an Output
pinMode(redLED, OUTPUT); //Defines redLED as an Output
}
//
void loop() {// Loop repeats itself the whole time until you shut your controller down
digitalWrite(blueLED, HIGH); //turns blueLED on
delay(delaytime);//arduino sleeps for delaytime (currently at 1000 ms)
digitalWrite(blueLED, LOW);//turns blueLED off
digitalWrite(redLED,HIGH);//turns redLED on
delay(delaytime);//arduino sleeps again for delaytime
digitalWrite(redLED, LOW);//turns redLED off
//Loop repeats itself
}

And don't forget to upvote and resteem!!! 😛

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.029
BTC 57207.57
ETH 2995.57
USDT 1.00
SBD 2.26