Let’s write simple program, that will turn on and off LED on our Arduino. For our lesson we need: Arduino, one LED diode, USB cable for programming our Arduino, PC with Arduino IDE, download it you can here: https://www.arduino.cc/en/software
Connect USB cable to Arduino, for connecting LED we will use D13 pin and GND PIN (see Arduino Nano pins here). Positive wire of diode connect to D13, negative wire – to Ground pin as you can see on photo:
In Arduino IDE create new blank project:
Here You can see two functions: setup() – which run when Arduion power on, and function loop() – which run in cycle all type when arduino is working. Now let’s create our program.
Bewore function start() let’s create two constants – it will be basic configuration of our program. BLINK_PIN defines which pin is used for connecting LED. BLINK_TIME – define time in milliseconds how long our LED will be on and off.
#define BLINK_PIN 13 #define BLINK_TIME 1000
In function setup() configure pin 13 as output pin:
void setup() { pinMode(BLINK_PIN,1); }
Now create function blink_led() – that will turn on and off our LED:
void blink_led() { digitalWrite(BLINK_PIN, 1); delay(BLINK_TIME); digitalWrite(BLINK_PIN, 0); delay(BLINK_TIME); }
Now write function blink_led() in loop() to run our program in cycle all time when Arduino works:
void loop() { blink_led(); }
Full code of program:
#define BLINK_PIN 13 #define BLINK_TIME 300 void blink_led() { digitalWrite(BLINK_PIN, 1); delay(BLINK_TIME); digitalWrite(BLINK_PIN, 0); delay(BLINK_TIME); } void setup() { pinMode(BLINK_PIN,1); } void loop() { blink_led(); }
Now save program and compile it to your Arduino: