0% found this document useful (0 votes)
12 views

Project 4 Controlling LED by Button Module

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Project 4 Controlling LED by Button Module

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Project 4:Controlling LED By Button Module

Description:
In this project, we will control LED to light on and off via button module. When
the button is pressed, the signal end outputs low level (0); when released, the
signal end of sensor keeps high level(1).
Equipment:

Connection:
key module:
GND -- GND
VCC -- 5V
OUT -- D4

LED:
GND -- GND
SIG -- D5

Test Code:
Next to design the program, we make LED on by button. Comparing with
previous experiments, we add a conditional judgement statement. We use if
statement. The written sentences of Arduino is based on C language,
therefore, the condition judgement statement of C is suitable for Arduino, like
while, swich, etc.
For this lesson, we take simple “if” statement as example to demonstrate:
If button is pressed, digital 4 is low level, then we make digital 5 output high
level , then LED will be on; conversely, if the button is released, digital 4 is
high level, we make digital 5 output low level, then LED will go off.
As for your reference:

int ledpin = 5; // Define the led light in D5


int inpin = 4; // Define the button in D4
int val; // Define variable val
void setup ()
{
pinMode (ledpin, OUTPUT); // The LED light interface is defined as output
pinMode (inpin, INPUT); // Define the button interface as input
}
void loop ()
{
val = digitalRead (inpin); // Read the digital 4 level value and assign it to val
if (val == LOW) // Whether the key is pressed, the light will be on when
pressed
{digitalWrite (ledpin, HIGH);}
else
{digitalWrite (ledpin, LOW);}
}

Test Result:
This experiment is pretty simple, and widely applied to various of circuits and
electrical appliances. In our life, you could find this principle on any device,
such as the backlight is on when press any buttons, which is the typical
appliance.

You might also like