Attiny10 Digital Input read function not wrking

I am testing a code to read digital pin PB3 and turn on the relay on the ATtiny10. At the hardware level, I observe 0V and 5V when the switch is closed and open, respectively. However, I am unable to get the LED to blink when using the digital input. The LED blinks correctly when the digital input is not involved. Can you provide guidance on how to debug this issue?
I am using arduino version 1.8.9
Even i tried digtialRead

kindly note i want use PB3 only as digtial input. It has special function associated with it.

// Digital pin definitions
#define DIP_PIN 3 // Example digital pin (PB3)
 uint8_t dip4State = (PINB & (1 << DIP_PIN)) ? 1 : 0;
then try to readstatus of the pin

sample code .

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 1000000UL // Define CPU frequency as 1MHz

void InitIO() {
  DDRB |= (1 << PB2); // Set PB2 as output (Relay)
  DDRB &= ~(1 << PB3); // Set PB3 as input (Digital input)
  PORTB |= (1 << PB3); // Enable pull-up resistor on PB3
}

void RelayOn() {
  PORTB |= (1 << PB2); // Set PB2 high (Relay on)
}

void RelayOff() {
  PORTB &= ~(1 << PB2); // Set PB2 low (Relay off)
}

void RelayToggle() {
  PORTB ^= (1 << PB2); // Toggle PB2 (Relay on/off)
}

void DelayTime(uint32_t time_ms) {
  while (time_ms >= 1000) {
    _delay_ms(1000); // Delay for 1000 milliseconds
    time_ms -= 1000;
  }
  while (time_ms > 0) {
    _delay_ms(1); // Delay for remaining milliseconds
    time_ms--;
  }
}

int main() {
  InitIO();

  DelayTime(100);
  uint8_t buttonStatus = bit_is_clear(PINB, PB3); // Store status in variable
DelayTime(100);
  while (1) {

   
    if (buttonStatus) { // Check if PB3 is low (button pressed)
      RelayToggle(); // Toggle relay state if button is pressed
      DelayTime(100); // Wait for 100 milliseconds to debounce
    } else {
      RelayOff(); // Ensure relay is off if button is not pressed
    }
    
    _delay_ms(10); // Small delay to debounce input
  }

  return 0;
}

Move the buttonStatus = bit_is_clear(PINB, PB3); inside the loop so it continuously checks the button's status:

int main() {
  InitIO();

  while (1) {
    uint8_t buttonStatus = bit_is_clear(PINB, PB3); // Check button status inside loop
    
    if (buttonStatus) { // Check if PB3 is low (button pressed)
      RelayToggle(); // Toggle relay state if button is pressed
      DelayTime(100); // Wait for 100 milliseconds to debounce
    } else {
      RelayOff(); // Ensure relay is off if button is not pressed
    }
    
    _delay_ms(10); // Small delay to debounce input
  }

  return 0;
}

1 Like

i already everything. But forum says some fuse setting to be done. We cant use this pin directly as digital input . we need to permanently disable reset feature and use as digital pin. But not mentioned how we should in arduino uno.
I have avrdude software there i can find fuse setting. but not able to flash the code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.