Shaped lamp – Learning path project 2

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.

Project 2

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.

2 - Shaped lamp - Components

You already know the brain, 4in1 Build&Code board, and the digital output LED module.

In this project we also use digital input modules or sensors so we can interact fisically with output modules

Blue led digital output

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?

2 - Shaping lamp - Circuit

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!

1.- Connect wire from 4in1 board DIO4 to LED

2.- Connect wire from 4in1 board DIO3 to Button

3.- Connect power or batteries to main board.

Pay attention to colors! should match. What do you think can happen if you reverse de board connector? Yes, sure it won't work! Why?

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.

2.1 Read button state. Conditional sentence

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.

Block Coding
Arduino Coding
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

Note we can use different sentences when reading binary variables or inputs. High state is same as number 1, and can be identified also as "true", so "if(X=HIGH)", "if(X=1)" or just"if(X)" is the same condition.

2.2 Power on LED with push button

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.

Block coding
Arduino code
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
  Puls = State % 2;  // even and odd? to get binary variable

  if (Puls == 0)
    digitalWrite (3, HIGH);
    digitalWrite (3, LOW);

How would you do for same action, but only change state with button long press? Probably you have to read input two times and then actuate, and remember you used delays before!

2.3 LED sequences by button state

See the code below, what do you think will happen??

Block coding
Arduino code
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);     
        digitalWrite(3, LOW);
        i = i + 1;

I think every time I press the button, LED will blink 4 times and remain off until new button press. No? Yes? You agree? Incredible!!

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?

Let's play!
2.4 LED and digital sensors

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!!

Final project - SHAPED LAMP

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!!

Block coding
Arduino code
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;

  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);
      digitalWrite(2, HIGH);

  if (modo == 3) {
    modo = 0;

previous arrow
next arrow

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