# Disco Light – Learning Path Project 1

Our Learning Path is an itinerary designed to teach all the foundaments and a little more about electronics, coding and robotics.

When you complete all the projects you will be able to develop almost any project you can imagine. In each chapter new concepts are introduced. You will learn by doing in a funny way.

With this activity you can be introduced to basics. How to start, turning on a led with your 4in1 board.

Project 1
DISCO LIGHT

Learn how to power on a led, control time to get intermitent lighting, and regulate brightness, like a real disco light!

BEFORE TO START

DIFFICULTY LEVEL: Beginner
DURATION OF THE ACTIVITY: 30 min.

MATERIALS:

1
Build&Code 4in1 board

Battery holder, USB cable and wires.

1 - Disco light - Components

The monochromatic LED is a module that puts out light of some fixed colour. You can use blue, green, yellow, red or white led modules for this exercise.

The 4in1 Build&Code circuit board is the brain, and we have to tell him what to do. How? Well, we can do it in different ways, grafically using blocks language or coding. In both cases we need to download a free software able to program our brain:

Mblock

Scratch inspired graphical programming language software by blocks

Arduino IDE

It makes it easy to write code and upload it to the board.

Check First steps page to connect your board properly or if you have some issue loading programs.

1. Power on one LED - Circuit

The 4in1 Build&Code circuit board has 12 digital ports (I/O). We can set each port to be an input, or an output.

Next step is to connect components. We are using Direct Current (DC) power supply with maximum 12V, so it's completly safe for us.
Each digital component has 3 pins: voltage (V) and ground (G) to power it, and signal (S) that could be in "high" (5V) or "low" (0V) state.

Connect wire from 4in1 board DIO3 to LED module according colours:

- Black: Ground
- Red: Voltage
- Yellow: Signal

1.1 Power on one LED - Program

To power a led, as any other digital actuator, we need to define an output port, and set it to "high" or "low" level state, setting it with a value "1" or "0", "on" or "off"

Arduino Coding
```int LED = 3;             // CONNECT LED TO DIGITAL IO 3

void setup() {
pinMode(LED, OUTPUT);   // DEFINE AS OUTPUT
}

void loop() {
digitalWrite(LED, HIGH);  // POWER ON LED
}```
1.2 Flashing LED

You know how to power on a LED, now, we want it to flash! We will define 1 sec ON and 1 sec OFF continously

Using the same circuit, we will add some code:

Block coding
Arduino code

```int LED = 3; // DEFINE LED DIGITAL OUTPUT TO DIO 3

void setup() {
pinMode(LED, OUTPUT);   // DEFINE DIO PIN AS OUTPUT
}

void loop() {
digitalWrite(LED, HIGH);  // POWER ON LED
delay(1000);              // WAIT ONE SECOND (1000ms = 1s)
digitalWrite(LED, LOW);   // POWER OFF LED
delay(1000);              // WAIT ONE SECOND (1000ms = 1s)
}```

Try to get different speeds, modify time from 1 second to 1/4s, 2s and 10 s.
Probably you need a different flash rythm. Do it modifying time of "HIGH" and "LOW" states.

1.3 Flashing LED - Variables

When coding, we can use variables, a symbolic name associated with a value that can be changed easily in definition, or during progam execution.
We need to define it, assign a initial value, and use that name instead of numbers directly in our functions. Try it!

Arduino code
```int LED = 3; // DEFINE LED TO DIO 3
int time1 = 3000;  //DEFINE VARIABLE WITH TIME POWERED ON/OFF IN MILISECONDS
void setup() {
pinMode(LED, OUTPUT);   // DEFINIR PIN COMO SALIDA
}

void loop() {
digitalWrite(LED, HIGH);  // POWER ON LED
delay(time1);             // WAIT MILISECONDS DEFINED IN TIME1 VAR
digitalWrite(LED, LOW);   // POWER OFF LED
delay(time1);             // WAIT MILISECONDS DEFINED IN TIME1 VAR
}```

If you want to have different rythm, you will need to add a second variable, so "on" time and "off" time are different. Do it!

1.4 LED brightness - PWM

Leds are ON or OFF components, as digital output, they can not be half powered to get less brightness. The solution is flashing it very fast, getting fast pulses that our eyes cannot see. As fast as 25000 pulses every second!

Adjusting different time for "LOW" and "HIGH" state, we can perceive different led brightness. This is called pulse-width modulation (PWM). This is also used to control other devices, we will see in next activities.

The 4in1 board has also analog outputs, where numeric values from our code is converted to variable voltage level. In our program we can set it from 0 to 255 value, and voltage output will be from 0 to 5 Volts Internally this is done using PWM and some electronics (low pass filter)

Interesting but....
I want to code!
1.4 LED Brightness - PWM
Block coding
Arduino code
```int LED = 3;      // DEFINE LED DIGITAL OUTPUT TO DIO 3
int brillo = 0;   // VARIABLE TO DEFINE LED BRIGHTNESS
void setup() {
pinMode(LED, OUTPUT);   // DEFINE DIO PIN AS OUTPUT
}

void loop() {
analogWrite(LED, brillo);  // ADJUST PWM ACCORDING BRIGHTNESS VARIABLE
delay(250);                // WAIT 250 MILISECONDS
brillo = brillo + 10;      // INCREMENT VARIABLE VALUE IN 10
}```

Arduino has a function specifically designed to do PWM over any digital output. It just need two values, digital pin and PWM value from 0 to 255

Final project - DISCO LIGHTS

Now you can add all your led modules, and do them shine as you want! You can download cardboard templates to build it, and see below an example code. Feel free to use different combinations, different times for each led....

Block coding
Arduino code
```int led_blanco = 2;
int led_rojo = 3;
int led_verde = 4;
int led_amarillo = 5;
int led_azul = 6;

void setup() {
pinMode(led_blanco, OUTPUT);
pinMode(led_rojo, OUTPUT);
pinMode(led_verde, OUTPUT);
pinMode(led_amarillo, OUTPUT);
pinMode(led_azul, OUTPUT);
}

void loop() {
for (int i = 0; i < 10; i++) {
delay(200);
digitalWrite(led_blanco, LOW);
delay(200);
digitalWrite(led_blanco, HIGH);
}

int brillo = 0;
while (brillo > 255) {
brillo = brillo + 5;
analogWrite(led_rojo, brillo);
delay(100);
}

digitalWrite(led_verde, HIGH);
digitalWrite(led_amarillo, HIGH);
digitalWrite(led_azul, HIGH);
digitalWrite(led_azul, LOW);
digitalWrite(led_amarillo, LOW);
digitalWrite(led_verde, LOW);

brillo = 255;
while (brillo < 0) {
brillo = brillo - 5;
analogWrite(led_rojo, brillo);
delay(100);
}

}```

The Mega Maker Kit fits perfectly with the Learning Path, you can build all projects with it, but if you have other kits, you can also follow the entire itinerary and finish some projects, or buy the missing components. You can check in our Learning Path page.

0 0

0