logo elektroda
logo elektroda
X
logo elektroda

ATTiny2313 AnalogRead Not Working for LDR and PIR Sensor Project—Why?

243 12
ADVERTISEMENT
  • #1 21671942
    Jay Reyes
    Anonymous  
  • ADVERTISEMENT
  • #2 21671943
    Neil Mula
    Anonymous  
  • #3 21671944
    jay reyes
    Anonymous  
  • ADVERTISEMENT
  • #4 21671945
    Neil Mula
    Anonymous  
  • #5 21671946
    jay reyes
    Anonymous  
  • ADVERTISEMENT
  • #6 21671947
    jay reyes
    Anonymous  
  • #7 21671948
    Neil Mula
    Anonymous  
  • #8 21671949
    jay reyes
    Anonymous  
  • #9 21671950
    jay reyes
    Anonymous  
  • ADVERTISEMENT
  • #10 21671951
    Neil Mula
    Anonymous  
  • #11 21671952
    jay reyes
    Anonymous  
  • #12 21671953
    Neil Mula
    Anonymous  
  • #13 21671954
    jay reyes
    Anonymous  

Topic summary

✨ The discussion addresses an issue with using analogRead on an ATTiny2313 microcontroller for a night-activated project involving an LDR and a PIR sensor. The project works correctly on an Arduino but fails on the ATTiny2313, where analog inputs do not function as expected, causing the LED and buzzer to activate immediately upon powering the IC. Troubleshooting steps included verifying circuit connections, testing with and without the PIR sensor, changing resistor values, and trying alternative code. The key insight revealed is that the ATTiny2313 lacks a built-in Analog-to-Digital Converter (ADC), so it cannot perform analogRead operations directly. This limitation explains why analog input readings fail on the ATTiny2313. Suggested solutions include adding an external ADC via SPI, switching to a microcontroller with built-in ADC like an ATmega, or using the PIR sensor as a digital input since it typically outputs digital signals. The LDR readings referenced (values around 500) correspond to digital bit values from ADC conversion on Arduino platforms, which the ATTiny2313 cannot replicate natively.

FAQ

TL;DR: Your ATtiny2313 has 0 ADC channels, so analogRead won’t work. “The ATTiny2313 AVR does not have ADC.” Use a digital PIR and either add an SPI ADC or switch MCUs. [Elektroda, Neil Mula, post #21671953]

Why it matters: This FAQ helps makers debug night‑triggered LDR/PIR builds that fail after moving from Arduino boards to an ATtiny2313.

Quick Facts

Why doesn’t analogRead work on my ATtiny2313 LDR/PIR project?

Because ATtiny2313 has no ADC. Without an analog‑to‑digital converter, analogRead cannot sample the LDR voltage, so your threshold logic never evaluates correctly. As Neil put it, “The ATTiny2313 AVR does not have ADC.” Use a digital PIR and add an external SPI ADC or change MCU. [Elektroda, Neil Mula, post #21671953]

Does a PIR sensor need an analog input here?

No. Many hobby PIR modules provide a single digital OUT pin that goes HIGH on motion. Wire it to a digital input and skip analogRead entirely on ATtiny2313. This matches the Arduino Playground‑style PIR modules Neil referenced. [Elektroda, Neil Mula, post #21671953]

Why do my LED and buzzer turn on immediately when I power the ATtiny2313?

Your sketch expects a valid analog threshold, but the chip lacks ADC. At power‑up, your code likely treats the unmeasurable LDR path as a trigger, so the LED and buzzer go HIGH right away. That exact symptom was reported in this thread. [Elektroda, Jay Reyes, post #21671942]

Can I add ADC capability to the ATtiny2313?

Yes. Use an external ADC (e.g., SPI‑interfaced). The ATtiny2313 exposes SPI, so you can read the LDR via that converter and keep your small MCU. Expect added cost and board area compared with picking a chip that already includes ADC. [Elektroda, Neil Mula, post #21671953]

Should I switch to an ATmega instead of bolting on an external ADC?

Often yes. An ATmega with onboard ADC simplifies wiring and code and keeps costs near an external‑ADC solution. Neil noted the cost gap is small, and integration is easier with built‑in ADC channels. [Elektroda, Neil Mula, post #21671953]

What does “less than 500” mean in my LDR readings?

That number is an ADC count from the board where your code originally worked. Neil explained it as digital bits representing voltage (e.g., 0–255 style). The ATtiny2313 cannot produce that number without an ADC, so the comparison fails. [Elektroda, Neil Mula, post #21671953]

How do I quickly isolate the fault before redesigning?

Try this 3‑step check: 1. Remove the PIR module and test. 2. Move the PIR to another I/O pin and retest. 3. Adjust the LDR threshold in code and observe behavior. Neil suggested these steps to pinpoint the failing path. [Elektroda, Neil Mula, post #21671945]

I copied code from a comment and got compile errors—what happened?

Forum formatting can inject stray Unicode characters (like en‑dashes), breaking Arduino IDE compilation. Use the attached .ino or retype the lines to remove hidden characters. Jay reported stray character errors traced to formatting. [Elektroda, jay reyes, post #21671947]

My PIR is removed, but the issue persists—what next?

That confirms the PIR isn’t the root cause. The failure stems from relying on analogRead on a microcontroller without ADC. Rework the design: use a digital PIR and either add an SPI ADC for the LDR or switch MCUs. [Elektroda, jay reyes, post #21671947]

Does my 30 kΩ resistor next to the LDR affect the problem?

Component values set the light threshold, but the core issue is lack of ADC on ATtiny2313. You can optimize the divider later; first restore a valid analog measurement path via an external ADC or a different MCU. [Elektroda, jay reyes, post #21671950]

What is an ADC, in plain language?

An ADC (analog‑to‑digital converter) turns voltages from parts like LDRs into numeric counts for code comparisons. Without one, analogRead comparisons such as “<500” cannot run on ATtiny2313. “The ATTiny2313 AVR does not have ADC.” [Elektroda, Neil Mula, post #21671953]

How can I port my working Arduino prototype to ATtiny2313 cleanly?

Keep the PIR on a digital pin. For the LDR path, add an SPI ADC and map its reading to your threshold. Your Arduino‑side success shows the logic is fine; you just need equivalent ADC hardware on ATtiny2313. [Elektroda, Jay Reyes, post #21671942]
ADVERTISEMENT