Skip to product information
1 of 2

vendor-unknown

How To Write Arduino Code For Beginners - KT799

How To Write Arduino Code For Beginners - KT799

SKU:KT799

999 in stock

Regular price Rs. 650.00
Regular price Sale price Rs. 650.00
Sale Sold out
Shipping calculated at checkout.

For refund/return/replacement, call us at +91 95995 94524 For bulk and B2B enquiries kindly mail us at support@rees52.com

  • Fast Shipping
  • Affordable Price
  • Support
View full details

KIT INCLUDES:


HARDWARE REQUIRED

SOFTWARE REQUIRED

Arduino IDE 1.8.5 (programmable platform for Arduino)

Click To Download: https://www.arduino.cc/en/Main/Software

SPECIFICATIONS

ARDUINO UNO

  • Operating Voltage: 5v
  • Input Voltage: 7-20v
  • Digital I/O Pins: 14 (of which 6 provide PWM output)
  • Analog Input Pins: 6
  • DC Current per I/O Pin: 20 mA
  • DC Current for 3.3V Pin: 50 mA
  • Flash Memory: 32 KB of which 0.5 KB used by bootloader
  • SRAM: 2 KB
  • EEPROM: 1 KB
  • Clock Speed: 16 MHz

PIN DESCRIPTION

ARDUINO UNO

LED

CIRCUIT CONNECTION AND WORKING

In this circuit, the anode of the LED is connected to 13th pin (or any other digital pin for that matter) of the Arduino UNO through a current limiting resistor of 1 Kilo Ohms. The cathode of the LED is connected to ground.

The next step is to connect a 10 Kilo Ohm resistor between 10th pin of Arduino UNO and 5V supply i.e. the 10th pin of the Arduino is pulled high. Hence, 10th pin of the Arduino will continuously detect logic HIGH.

Now connect one end of the switch or button to the 10th pin of the Arduino and the other end is connected to ground. The power supply to the breadboard can be given from 5V and GND pins of the Arduino UNO. This finishes the design of the circuit.

Now, we will see the theory behind the button press. As mentioned earlier, the 10th pin of the Arduino is pulled high and as a result, it continuously detects logic HIGH. Also, the button is connected between pin 10 and ground.

Hence, if the button is pressed, the connection between the pin 10 of Arduino and ground is closed and as a result, Arduino will detect a logic LOW on the 10th pin. With the help of this transition from Logic HIGH to Logic LOW, we can detect if the button is pressed or not and subsequently turn the LED on or off.

As the circuit is successfully described with its design and theory, the next step is to write the program or sketch for this circuit and logic in the Arduino IDE.

Writing the Sketch for the circuit

For writing the code easily, we need to follow the following steps.

  • Initialize a pin as output for the LED.
  • Initialize a pin as input for the button or switch.
  • Detect the status of the button.
  • Turn the LED on or off.

So, the first step is to initialize a pin (Pin 13) of the Arduino as output for LED and another pin (Pin 10) as input for button. As we have seen in the previous tutorial, we need to use the function pinMode to initialize a pin as input or output. Hence, write the pinMode functions for both LED and button pin in the setup function.

IMAGE 1

Once we initialized the pins for LED and switch, the next step is to check for the status of the switch. In order to do this, we need to use a function called digitalRead ();

digitalRead, as the name specifies, is a function which is used to read values from digital pins i.e. it reads for logic LOW or HIGH at the digital pins.

The syntax of digitalRead function is digitalRead (pin no);

In the syntax, pin no indicates the number of the digital pin from which you want to read the digital data. The digitalRead function can return two values: HIGH or LOW. If the digitalRead detects 5V at the pin, then HIGH is returned. Similarly, if the digitalRead detects 0V at the pin, then LOW is returned.

Coming to our circuit, we need to read the status of the button pin i.e. 10th pin. Create a variable before setup and name it as “buttonstate”, so that the value returned by the digitalRead function can be captured. Now in the loop function, assign the return value of the digitalRead of 10th pin to the buttonstate variable.

IMAGE 2

With this step, we now have captured the status of the button in a variable. The next step is to control the LED. In order to control the LED i.e. either turn it on or off, we need to use a conditional statement called if and else.

IMAGE 3

If – else statement is used to check for a condition and execute a block of code in the program. If the condition in the “if” statement is true, the block of code corresponding to the “if” statement is executed. If the condition in the “if” statement is false, then the block of code in the “else” statement is executed.

So, in the “if” statement, we compare the buttonstate variable with LOW. This is because, the Arduino UNO will detect a logic LOW when the button is pressed. If this condition is true i.e. if the button is pressed, then we turn on the LED by writing digitalWrite function to output high on LED pin in the “if” statement.

If the condition is false i.e. if the button is not pressed, then we turn of the LED by writing digitalWrite function to output low on the LED pin in the “else” statement.

IMAGE 4

Now, you need to verify the code and then upload the same. When you push the Button on the Breadboard then it turn on the LED by writing digitalWrite function to output high on LED pin in the “if” statement and the LED starts blinking.