• EUR€
  • £GBP
  • $USD

Arduino Projects 5: Mini Lamp

DFRobot Jun 22 2017 23

A lamp is something we use on a daily basis, usually made up of a light source and a simple switch. Let’s make our own.

Parts Needed:

DFRduino Uno   (similar as Arduino UNO R3)  x1
I/O Sensor Expansion Shield V7.1  x1
Digital Push Button Module  x1 
Digital Piranha  LED-R  x1 


Connect the Digital Push Button to digital pin 2
Connect the Digital Piranha LED-R to digital pin 13
Be sure that your power, ground and signal connections are correct or you risk damaging your components.
When you have connected the components and checked your connections, connect the arduino microcontroller to your PC with the USB cable so you can upload a program.

Hardware Analysis
(Digital Input – Digital Output)

In the control device we have made:

The digital push button is the input unit and the LED is the output unit. Like our last
experiment, we have one digital input and one digital output.

Code Input

//Sample Code 3-1: Mini Lamp
int buttonPin = 2;
int ledPin = 13;
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
void loop() {
int reading = digitalRead(buttonPin);
lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
lastButtonState = reading;

Use this sample code to implement the behavior we want.
You can copy and paste it in to the Arduino IDE, but if you want to develop you skills we recommend typing it out.
When you have finished, click “Verify” to check the code for syntax errors. If the code verifies successfully, you can upload it to your microcontroller. Once the code has uploaded, press the button. The LED should turn on.
When the button is pressed again, the LED should turn off.

Code Analysis

The button is the input device and the LED is the output device.
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite() reads the state of the button
int reading =
When the button switches between HIGH and LOW states, there will be a brief period of oscillation. This is visually represented below:

To avoid signal errors such as this, we will wait when the signal changes and read it some time after.
The function millis() will record the time when data collected has changed
if (reading != lastButtonState) {
lastDebounceTime =
millis() is a function which measures duration. It is measured in milliseconds (1 second = 1000 milliseconds). This particular function will start counting when the DFRduino UNO microcontroller (such as Arduino UNO)  is powered on.

Wait for another 50ms and check if the button’s signal has the same value with its current state. If not, change the state of the button. If the button is in the state of HIGH (meaning the button has been
pressed), change the state of LED.

if ((millis() - lastDebounceTime) >
debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;


People are always listening to music on their headphones.
This means that if they are at home and the doorbell rings, they cannot hear it.

A doorbell with a mini lamp would solve this problem. If the doorbell is pressed, the lamp will light up. Why not try and add this feature to your own doorbell at home?

There is also another arduino starter kit for you to choose.