{"id":3638,"date":"2023-06-27T16:08:20","date_gmt":"2023-06-27T16:08:20","guid":{"rendered":"https:\/\/projectkitsandparts.com\/?p=3638"},"modified":"2023-09-23T04:35:32","modified_gmt":"2023-09-23T04:35:32","slug":"interfacing-1-key-input-button-to-control-1-led","status":"publish","type":"post","link":"https:\/\/projectkitsandparts.com\/index.php\/2023\/06\/27\/interfacing-1-key-input-button-to-control-1-led\/","title":{"rendered":"Interfacing 1-Key Input Button to Control 1-LED"},"content":{"rendered":"\n<p>Project Overview: In this project, we will demonstrate how to interface a 1-key input button with an Arduino board to control the ON\/OFF state of an LED. Each button press will toggle the LED&#8217;s state.<\/p>\n\n\n\n<p>Hardware Required:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>Arduino board (e.g., Arduino Uno)<\/li>\n\n\n\n<li>4-key input button module<\/li>\n\n\n\n<li>LED Module<\/li>\n\n\n\n<li>Jumper wires<\/li>\n<\/ol>\n\n\n\n<p><strong>Circuit Diagram: Connect the components as follows:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/projectkitsandparts.com\/wp-content\/uploads\/2023\/07\/Circuit-Diagram-2.png\" alt=\"\" class=\"wp-image-3639\" style=\"width:400px\" width=\"400\" srcset=\"https:\/\/projectkitsandparts.com\/wp-content\/uploads\/2023\/07\/Circuit-Diagram-2.png 715w, https:\/\/projectkitsandparts.com\/wp-content\/uploads\/2023\/07\/Circuit-Diagram-2-300x209.png 300w, https:\/\/projectkitsandparts.com\/wp-content\/uploads\/2023\/07\/Circuit-Diagram-2-220x154.png 220w, https:\/\/projectkitsandparts.com\/wp-content\/uploads\/2023\/07\/Circuit-Diagram-2-600x417.png 600w\" sizes=\"(max-width: 715px) 100vw, 715px\" \/><\/figure>\n\n\n\n<p>Note: The specific pin connections may vary depending on the type of 4-key input button module you have. Please refer to the datasheet or documentation of your button module for the appropriate connections.<\/p>\n\n\n\n<p><strong>Connection Table: Here&#8217;s a connection table for the button module, LED, and Arduino:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Button Module Pin<\/td><td>Arduino Pin<\/td><\/tr><tr><td>Key1 VCC GND<\/td><td>Digital Pin (e.g., 8) Vin(vcc) GND<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>LED Module Pin<\/td><td>Arduino Pin<\/td><\/tr><tr><td>ledPin1 Vcc<\/td><td>Digital Pin (e.g., 5) 5v(vcc)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Below is the Arduino code to control the LED using the key input button:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/projectkitsandparts.com\/\" data-type=\"link\" data-id=\"https:\/\/projectkitsandparts.com\/\"><strong>\/\/www.projectkitsandparts.com<\/strong><\/a><\/p>\n\n\n\n<p>\/\/ Define the button module pins connected to the Arduin o<\/p>\n\n\n\n<p>const int keyPin1 = 8;<\/p>\n\n\n\n<p>\/\/ Define the LED pins connected to the Arduino<\/p>\n\n\n\n<p>const int ledPin1 = 5;<\/p>\n\n\n\n<p>void setup() {<\/p>\n\n\n\n<p>&nbsp; \/\/ Set the button module pins as inputs<\/p>\n\n\n\n<p>&nbsp; pinMode(keyPin1, INPUT_PULLUP);<\/p>\n\n\n\n<p>&nbsp; \/\/ Set the LED pins as outputs<\/p>\n\n\n\n<p>&nbsp; pinMode(ledPin1, OUTPUT);<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>void loop() {<\/p>\n\n\n\n<p>&nbsp; \/\/ Read the button module pins<\/p>\n\n\n\n<p>&nbsp; bool buttonState1 = digitalRead(keyPin1);<\/p>\n\n\n\n<p>&nbsp; \/\/ Check if any button is pressed<\/p>\n\n\n\n<p>&nbsp; if (buttonState1 == LOW) {<\/p>\n\n\n\n<p>&nbsp; &nbsp; \/\/ If button 1 is pressed, turn on LED 1<\/p>\n\n\n\n<p>&nbsp; &nbsp; digitalWrite(ledPin1, LOW);<\/p>\n\n\n\n<p>&nbsp; &nbsp; delay(200);<\/p>\n\n\n\n<p>&nbsp; } else {<\/p>\n\n\n\n<p>&nbsp; &nbsp; \/\/ If no button is pressed, turn off all LEDs<\/p>\n\n\n\n<p>&nbsp; &nbsp; digitalWrite(ledPin1, HIGH);<\/p>\n\n\n\n<p>&nbsp; }<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Code Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>The code defines the pins connected to the button module (keyPin1) and the LED pins (ledPin1).<\/li>\n\n\n\n<li>In the setup() function, the button module pins are set as inputs using pinMode(), and the LED pins are set as outputs.<\/li>\n\n\n\n<li>In the loop() function, the state of each button is read using digitalRead() and stored in respective variables (buttonState1, buttonState2, etc.).<\/li>\n\n\n\n<li>The code then checks which button is pressed by comparing the button states.<\/li>\n\n\n\n<li>If a button is pressed, the corresponding LED is turned on by setting its pin to LOW using digitalWrite(), and a delay of 200 milliseconds is added for debounce.<\/li>\n\n\n\n<li>If no button is pressed, all LEDs are turned off by setting their pins to HIGH.<\/li>\n\n\n\n<li>The code continuously loops and checks the button states, allowing the LEDs to be controlled based on the button presses.<\/li>\n<\/ol>\n\n\n\n<p><strong>Project Steps:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connect the Arduino board to your computer via USB.<\/li>\n\n\n\n<li>Open the Arduino IDE (Integrated Development Environment) on your computer.<\/li>\n\n\n\n<li>Create a new sketch and copy-paste the code provided into the IDE.<\/li>\n\n\n\n<li>Make sure the correct Arduino board and port are selected under the &#8220;Tools&#8221; menu.<\/li>\n\n\n\n<li>Click the &#8220;Upload&#8221; button to compile and upload the code to the Arduino board.<\/li>\n\n\n\n<li>Connect the 4-key input button module to the Arduino using the specified pin connections.<\/li>\n\n\n\n<li>Connect the LEDs to the Arduino&#8217;s digital pins specified in the connection table.<\/li>\n\n\n\n<li>When the code runs, pressing each button will turn on the corresponding LED, and releasing the button will turn off the LED.<\/li>\n\n\n\n<li>If no button is pressed, LED will be turned off.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Project Overview: In this project, we will demonstrate how to interface a 1-key input button with an Arduino board to control the ON\/OFF state of an LED. Each button press will toggle the LED&#8217;s state. Hardware Required: Circuit Diagram: Connect the components as follows: Note: The specific pin connections may vary depending on the type &hellip; <a href=\"https:\/\/projectkitsandparts.com\/index.php\/2023\/06\/27\/interfacing-1-key-input-button-to-control-1-led\/\" class=\"more-link\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":3641,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[415],"tags":[429,418,435,432,439,430],"class_list":["post-3638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","tag-arduino","tag-arduino-uno","tag-cool-mad-kits","tag-dolphin-labs","tag-one-key-interfacing","tag-project-kits"],"_links":{"self":[{"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/posts\/3638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/comments?post=3638"}],"version-history":[{"count":3,"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/posts\/3638\/revisions"}],"predecessor-version":[{"id":3775,"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/posts\/3638\/revisions\/3775"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/media\/3641"}],"wp:attachment":[{"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/media?parent=3638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/categories?post=3638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/projectkitsandparts.com\/index.php\/wp-json\/wp\/v2\/tags?post=3638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}