2. Basics¶
In this section we are going to see the Arduino equivalent of the scripts we ran from the Raspberry Pi.
2.1. Example: Blink¶
We are going to run the “blink” sketch we have seen early on in this tutorial. It is the most basic sketch a sort of “Hello World!” for Arduino. It makes the built-in LED on pin 13 blink in intervals of 1 second.
- Connect the Arduino to your laptop with the USB cable
- Open the IDE
- Click Tools → Serial Port and select the USB serial port to which your Arduino is connected to (the path changes with operating system and USB port you are using, so the name might be different for you).
- Then, select the right board: click Tools → Board → Arduino Uno.
- Then you can open the basic sketch “Blink” by clicking on File → Example → 01. Basics → Blink.
- You can then upload the sketch on the Arduino by clicking the “Upload” button (the one with a right arrow).
- Once uploaded you will see the LED on pin 13 blink.
2.1.1. Alternative Hardware¶
You might want to try to use an external LED. Here’s the wiring diagram:

2.1.2. Understanding the “Blink” code¶
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
pinMode(LED_BUILTIN, OUTPUT);
here we are initialising pin 13 as a output pin, note that instead of using a pin number we are using theLED_BUILTIN
constant that stands for “pin 13”digitalWrite(LED_BUILTIN, HIGH);
here we are turning the LED on by setting the digital output asHIGH
(HIGH is maximum voltage level, 5V)delay(1000);
here we are waiting for a second, note that the delay() function takes as a parameter millisecondsdigitalWrite(LED_BUILTIN, LOW);
here we are turning the LED off by making the voltageLOW
(0V)delay(1000);
here we are waiting for a second again
We have setup general instructions in our setup, those instructions won’t change when our sketch is running. We have inserted all the functions in the main loop so that they can be repeated infinitely.
Try to tweak the delays to see how the timing differs.
2.2. Example: Led PWM¶
Here we will see how to do pulse-width modulation with the Arduino using a LED.
2.2.2. Code¶
For the code you can upload the built-in example “Fade” from File → Example → 01. Basics → Fade.
2.2.3. Understanding the “Fade” code¶
int led = 9;
int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
int led = 9;
here we are creating a variable of type int with nameled
and storing in it the pin number that our LED is connected to, note that we are using pin number 9 which is one of the PWM-capable pins (marked by the ~ sign).int brightness = 0;
here we are creating a variable of type int with namebrightness
and assigning the initial value of0
int fadeAmount = 5;
here we are storing the amount we want the LED to fade for each interval in thefadeAmount
variablepinMode(led, OUTPUT);
here we are declaring the led pin as an output note that this would be equivalent to thispinMode(9, OUTPUT);
analogWrite(led, brightness);
here we are writing on pin 9 (led
) the brightness valuesbrightness = brightness + fadeAmount;
here we are adding a fadeAmount to the brightness levelThen
if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; }
checks that the brightness level never takes invalid values (below 0 or above 255)
delay(30);
a short delay to make the dimming effect more visible
2.3. Example: Button¶
Here we are going to see how Arduino receives signals from input devices using a button.
2.3.2. Code¶
For the code you can copy and paste the following code:
int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
int buttonState = digitalRead(pushButton);
Serial.println(buttonState);
delay(1);
}
Serial.begin(9600);
here we are opening the serial communication with a baud rate of 9600 BaudspinMode(pushButton, INPUT);
here we are setting the button’s pin as inputint buttonState = digitalRead(pushButton);
here we are reading the voltage of the button and memorising it in the variable buttonStateSerial.println(buttonState);
here we are printing the values in the Serial Monitordelay(1);
a short delay to stabilise the readings
To monitor what your Arduino is printing open the serial monitor by clicking on the serial monitor button:

2.4. Challenge¶
Important
You must demonstrate your build & code to the tutor team
We challenge you to combine the previous three sketches (Blink, Fade, Button) to create one that, with the press of the button, controls 2 LEDs such that:
- when the button is pressed one of the two LEDs fades to 25% of its brightness and the other one blinks once
- when the button is released the faded LED returns to 100% brightness.
You can find further help here.
Acknowledgements
Some material was taken from the Arduino website.