#1 Arduino Mega: GPIO Testing using Switch and LED | APDaga Tech

IoT Tutorial #1 [ Arduino Mega Tutorials #1 ] 
Here I will demonstrate Basic, a very first project using Arduino Mega (ATMega 2560).

Problem Statement:
GPIO Testing of the Arduino Mega using Switch and LED.

Requirements:
- Arduino Mega (ATMega 2560)
- USB Cable (A to B)
- 1 LED
- 1k Resistor
- 1 Switch (Push Button)
- Bread Board
- Male to Male wires (minimum 2)






Program Outcome:
When you press the Switch -> LED will Turn ON
Otherwise -> LED will remain OFF
Physical Connection:
Pin 2 -> Switch (pullup)(pressed means short to Gnd)(pressed->0, unpressed->1)
Pin 7 -> LED+
Gnd -> LED-
Downloads:
Download link is given in the Description of the YouTube video shown below.

Demonstration:
  



Program:
const int pin_led_out = 7;
const int pin_sw_in = 2;

void setup() {
// put your setup code here, to run once:
pinMode(pin_led_out, OUTPUT); //Set led_pin as Output
pinMode(pin_sw_in, INPUT_PULLUP); // set switch_pin as Input //Pullup means default state = HIGH
}

void loop() {
// put your main code here, to run repeatedly:
int sw_state = digitalRead(pin_sw_in);
if (sw_state == LOW) //If Switch is pressed
{
digitalWrite(pin_led_out, HIGH); //LED ON //5V
}
else
{
digitalWrite(pin_led_out, LOW); //LED OFF //0V
}
}

Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks and Regards,
-Akshay P. Daga
إرسال تعليق (0)
أحدث أقدم