I have a GP2Y0A21YK0F Sharp IR Analog Distance Sensor 10-80cm + Cable, Arduino Compatible and its is connected to my Arduino. All works well and I can read the analog data with the following code:

#include <SoftwareSerial.h>
#define RX 2 // receive
#define TX 3 // transmit
#define SEN1 4
SoftwareSerial mySerial =  SoftwareSerial(RX, TX);

void setup()  

  mySerial.println("Hello, world?");

void loop() // run over and over

   int reading = analogRead(SEN1);   


When the code runs on the Attiny with the GP2Y0A21YK0F IR Sensor connected, the Attiny84 resets over and over. The Attiny84 and GP2Y0A21YK0F are part of the same circuit e.g. Attiny and GP2Y0A21YK0F share the power and ground rail.

When I disconnect the GP2Y0A21YK0F, the program runs and stops resetting. I was able to reproduce this on an Attiny85 and ATMega328.

The strange thing is, it works fine on the Arduino. Any help is greatly appreciated.

  • 2
    Start by adding 100nF capacitors with short leads as close as possible to the the power pins. One near the ATtiny, one near the IR sensor. – jippie Sep 14 '14 at 5:36
  • +1 Thanks after some trial and error, placing a 100uf at the source and at the GP2Y0A21YK0F seems to work. Awesome :)! – PhillyNJ Sep 14 '14 at 12:00
  • 2
    You really want to use caps in the order of 100nF, that is nano Farad's. The 100uF, micro Farad's, may work now but may not keep up with age. It is fine to use them in parallel. The smaller 100nF caps have a much lower impedance for high frequencies than the 100uF caps and they don't age. – jippie Sep 14 '14 at 15:40

It sounds like the microcontroller is browning out.

IR sensors are notorious for taking a lot of power when they operate; often they will tax a small power supply and cause the voltage to droop low enough to reset/brownout the microcontroller.

Try either putting a fairly beefy capacitor on the power bus, or getting a larger power supply.

Hope this helps!

(edit: I reverted another user's edit to this answer; "droop" is in fact the correct term for what is going on: http://en.wikipedia.org/wiki/Voltage_droop)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.