Why LED is not turning OFF - 8051 Timers

trebla

Joined Jun 29, 2019
209
Check the button and LED are connected to right ports. Check MCU datasheet for output capabilities, are outputs push-pull or open collector type? Open collector output can only sink current.
 

trebla

Joined Jun 29, 2019
209
The second sentence under Port3 heading says that it is a push/ pull (sink/source). Sink means switching to ground and source means switching to Vdd. If you want LED on by writing 1 to output then you should connect LED in series with resistor between output pin and ground.

To test MCU working state is simplest method to write simple LED flashing program. If it works it means MCUs clock is working and output is connected correctly. Then you can test programs with buttons.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
To test MCU working state is simplest method to write simple LED flashing program. If it works it means MCUs clock is working and output is connected correctly. Then you can test programs with buttons.
I have already tested the LED. The LED works well. The problem can only be in the switch button and in the related code. But I do not understand how to find the real problem
 

trebla

Joined Jun 29, 2019
209
Test button with simple program: if button pressed, turn LED on, else turn LED off. Then you can see if input works and in which level activates LED output.
 

jpanhalt

Joined Jan 18, 2008
10,227
I have already tested the LED. The LED works well. The problem can only be in the switch button and in the related code. But I do not understand how to find the real problem
Here's a test circuit for your switch:

1599731140565.png

Does it work as expected? If yes, then problem is not switch. If no, get a new switch and start over at post #1. ;)
 

jpanhalt

Joined Jan 18, 2008
10,227
The problem can only be in the switch button and in the related code.
So now, you have determined the problem is in the code. Agree?

Let me suggest until you get the code working for everything except debouncing that you use a hardware debounce on the switch. IC's are made for that or a simple resistor/capacitor can be used.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
For this simple test program routine in #132 you do not need any debounce code. Simply read button and turn on LED.
I have tested following code When I press onboard switch button LED is not turning ON

C:
#include<reg51.h>

sbit LED = P2^0;
sbit Sensor = P3^7;


void main (void)
{
    P2 = 0x00;
    P3 = 0x80;

    while (1)
    {

      if (Sensor == 0)
       {
            LED = 1;

       }
      else
       {
           LED = 0;
       }
     }
}
When I change if (Sensor == 0) into if (Sensor == 1) still problem is same LED is not turning on
 

trebla

Joined Jun 29, 2019
209
No, in this code you must pull the pin down with button. Remember, MCU has internal pull-up resistor and button must then be between input pin and ground.
 

trebla

Joined Jun 29, 2019
209
Do you have a breadboard? You need set button on breadboard and take two wires to your MCU board, one to ground and second to MCU input.
 

JohnInTX

Joined Jun 26, 2012
4,281
According to the schematic in #139, you are sourcing current into the LED to turn it on. The 89C51 will only source 60uA at 2.4V typical. Consider driving your LED by sinking current to turn it on. PORT0 will sink more current than PORTS 1, 2 or 3 so think about that.

You show a 56 ohm resistor in the schematic and 10K in your breadboard photo, if I read it right. Which is it? How many mA do you need to drive the LED and what should the resistor value be?

The pin driving the LED is labeled '3'. Pin 3 on a DIP-40 package is P1.2 The code expects the LED on pin 21, P2.0

You can see if your I/O circuitry is working by using your meter on the pin. @trebla pointed out in #140 that your input pin doesn't change it's logic level when you push the button. That is something you could have determined by measuring the voltage at the pin while pushing the button on and off. Same with the LED output. You can see if your code in #138 is following the button by pulling the LED out of the circuit and just measuring the voltage at the pin as you push the button.
 
Last edited:

Thread Starter

Djsarakar

Joined Jul 26, 2020
317
The last one, #138. If you use different input do not forget modify your code also.
Code
C:
#include<reg51.h>

sbit LED = P2^0;
sbit Sensor = P0^7;


void main (void)
{
    P2 = 0x00;
    P0 = 0x80;

    while (1)
    {

      if (Sensor == 0)
       {
            LED = 1;

       }
      else
       {
           LED = 0;
       }
     }
}
Hardware Result - LED is turning ON without pressing switch button


IMG_20200910_175510.jpg
Mod edit: do not edit a post after someone has commented on it. Make a new post to keep the timeline valid - JohnInTX
 
Last edited by a moderator:
Top