How to manipulate a single bit of arduino

Description

LED is connected to pin 12 of Arduino. Write a code to toggle the LED continuously with a delay of 500ms.

I assume that you have gone through my previous blog post LED Interfacing.

Now in this blog, I will be manipulating a single bit of Arduino pin. Manipulating a bit means controlling a bit. making it HIGH(1) and LOW(0). To see the effect I will be connecting the LED to one of the pins to Arduino. I will connect the cathode to the ground. so by applying HIGH logic, LED will be turned ON and by applying LOW logic LED will be turned OFF.

As I stated in my previous post that we need to connect a resistor in between LED and Arduino as Port Pin I am using blue LED, I will be connecting resistor value of approximately 1K ohm.

Required components

  • Arduino UNO
  • Breadboard
  • Jumper wires
  • LED
  • Resistor 1K ohm
  • Power supply or DC Adapter

Where to place a resistor (at the cathode or at the anode side )?

In our circuit, it does not matter either way you can connect. The reason is we are connecting a resistor in series with the LED, and in series connection same current will flow through a resistor as well as LED.  But it should be present between MCU Pin and LED terminal if you don`t want to damage your LED. I have connected resistors to the Anode of LEDs.

Connection diagram

1] Resistor connected to the anode of the LED

pin12-to-r-to-gnd

2] Resistor connected to the cathode of the LED

pin12-to-led-to-r-gnd

As cathode is connected to Ground permanently, just by applying HIGH Logic to Pin 12 LED will be turned ON and by applying LOW Logic to Pin 12 LED will be turned OFF.

LED is an OUTPUT device. so first we need to declare that pin(where we have connected LED) as an OUTPUT. because we want to use pin 12 as an OUTPUT.

Now I will be doing setting the bit and resetting the bit after some interval of time. which means I need to have a little bit of delay so that I can see the change. so to generate a delay Arduino IDE gives us a function that we will be using.

Complementing logic(high and low) I will be doing inside loop function, so continuously it will turn on and turn off led after the delay which will be provided in code.

In Breadboard Connection and in the video I have connected a resistor to the Cathode of an LED.(figure 2).

Code

Code description

Line 01: Int LED = 12; Just declared one integer to assign pin number of Arduino.

Line 05: pinMode(LED, OUTPUT);  Declared pin 12 as output to connect LED.

Line 10: digitalWrite(LED, HIGH); Gave a high logic to pin12(LED), which turns ON LED.

Line 11,13: delay(500); Gave a delay of half-second(500ms).

Line 12: digitalWrite(LED, LOW); Gave a low logic to pin12(LED), which turns OFF LED.

Video

Leave a Comment

Your email address will not be published. Required fields are marked *