2. Basics

In this section we are going to see the Arduino equivalent of the scripts we ran from the Raspberry Pi.

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 name led 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 name brightness and assigning the initial value of 0

  • int fadeAmount = 5; here we are storing the amount we want the LED to fade for each interval in the fadeAmount variable

  • pinMode(led, OUTPUT); here we are declaring the led pin as an output note that this would be equivalent to this pinMode(9, OUTPUT);

  • analogWrite(led, brightness); here we are writing on pin 9 (led) the brightness values

  • brightness = brightness + fadeAmount; here we are adding a fadeAmount to the brightness level

  • Then

    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 Bauds
  • pinMode(pushButton, INPUT); here we are setting the button’s pin as input
  • int buttonState = digitalRead(pushButton); here we are reading the voltage of the button and memorising it in the variable buttonState
  • Serial.println(buttonState); here we are printing the values in the Serial Monitor
  • delay(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:

../_images/arduino-serial-monitor.png

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.