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 will know about digital inputs, using a button or swith and LED, doing different functions with them.
Build a lamp and learn different ways to use a button, or how to use other elements to interact with it
1 - Build&Code 4in1 board
5 - Monochromatic Led light
1 - Switch button module
1 - PIR motion sensor
1 - Finish line sensor
Battery holder, USB cable and wires.
Digital input sensors usually has HIGH state in signal pin when some happens over them. When buttons and switchs are pressed or PIR detects movement. Do you remember other digitals sensors?
The 4in1 Build&Code circuit board has 12 digital ports (I/O). We will set port 4 to be an output, and port 9 to be output, but remember, any DIO port can be used!
Voltage line is in the middle, so will be right anyway, no risk of damaging any component by reverse polarity. Module ground will be in signal pin, so module cannot be powered.
With digital sensors, we just need to define an input port and read his state ("high" or "low")
Most common is do something according the reading. We can use CONDITIONAL SENTENCES, for example, let's code: "if button is pressed, turn on the light, if not, turn off" using if/else sentence.

int PLED = 3; // White LED connected to digital port 3 int PButton = 4, ValueButton; // Button connected to digital port 4. Button reading variable void setup() { pinMode (PLED, OUTPUT); // Configuration of the LED as output signal pinMode (PButton, INPUT); // Configuration of the button as the input signal } void loop() { ValueButton = digitalRead(PButton); // Button status reading if (ValueButton == HIGH) // If the button is pushed digitalWrite(PLED, HIGH); // LED = ON else // If the button is not pushed digitalWrite(PLED, LOW); // LED off }
Now we want to change between LED states when button is pushed. So light will be turned on when button is pushed, and remains until button is pushed again to turn off light.
A variable can be used to save the lamp state, and actuate accordingly when button is pushed. Another option is read output state directly.

int State = 0; //Push button counter int Button = 0; //State of button int Puls = 0; //State of lamp (0 or 1) void setup() { pinMode(3, OUTPUT); // Digital port LED pinMode(4, INPUT); // Digital port Botton } void loop() { Button = digitalRead(4); if (Button == 1) //If button pressed { State = State + 1; //increment counter delay(300); } Puls = State % 2; // even and odd? to get binary variable if (Puls == 0) digitalWrite (3, HIGH); else digitalWrite (3, LOW); }
See the code below, what do you think will happen??
void setup() { pinMode(3, OUTPUT); //Define DIO3 as output pinMode(4, INPUT); //Define DIO3 as input } void loop() { while (true) { if (digitalRead(4) == HIGH) { //Read input state int i = 0; //reset i value to 0 while (i <= 3) { //4 time loop as i increases 1 every time digitalWrite(3, HIGH); delay(200); digitalWrite(3, LOW); delay(200); i = i + 1; } } } }
Are you able to get led blink with different patterns, when button is pressed fastly or with long press? Or maybe you prefeer to modify cadence in every press?
If you have PIR motion sensor module or Finish line sensor you can repeat previous exercises replacing button module.
Them work identically, but instead of our hand, other ambient conditions will activate light.
More public places has automatic lighting when somebody is near. You can do also with PIR motion sensor, it will detect movements and then, as the button coded before, will turn on light. You can add a delay to turn off after some time without movement.

PIR Sensor
Many machines use microswitch to control desplacements, limits of movement to safe operation or control position of moving parts.. Finish line sensor also uses a microswitch! Imagine you want a light warning every time a door is opened. You can do it!!
Now you can create your special and personalized lamp! It can shine as you decide!
Get cardboard templates to build it, and use the example code to start, but remember, your can adapt it to your needs!!
int modo = 0; void setup() { pinMode(2, OUTPUT); pinMode(13, INPUT); pinMode(11, INPUT); pinMode(12, INPUT_PULLUP); modo = 0; } void loop() { if (digitalRead(10)) { modo += 1; delay(700); } if (modo == 0) { digitalWrite(2, LOW); } if (modo == 1) { digitalWrite(2, HIGH); } if (!(digitalRead(12)) || digitalRead(11)) { digitalWrite(2, HIGH); } else { if (modo == 2) { digitalWrite(2, LOW); delay(200); digitalWrite(2, HIGH); delay(200); } } if (modo == 3) { modo = 0; delay(200); } }
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.