I’m having a problem reading my Sharp IR range sensor with my Pro Mini 5v.
#define IR 2
#define sampleSize 10
long interval = 100; // interval at which to blink (milliseconds)
long previousMillis = 0; // will store last time light sensor was updated
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you read the accelerometer!
previousMillis = currentMillis;
// Main code here!
int IRVal = sampleIRPin(IR);
int IRValMap = map(constrain(IRVal,82,419), 82, 419, 0, 100);
Serial.println(IRValMap);
}
}
int sampleIRPin(int IRPin)
{
long reading = 0;
analogRead(IRPin);
// Ignore first analog read
delay(1);
for (int i = 0; i < sampleSize; i++)
{
reading += analogRead(IRPin);
}
return reading/sampleSize;
}
My Pro Mini is on a breadboard with nothing else connected other than my 5v FTDI adapter. I’m also using a 10uF capacitor on the mains as the data sheet recommends. My readings are really unsteady and rarely rest at 0 which is what it should read when nothing is in sight. If I move the power for the sensor closer to the Pro Mini and not use the power rails of the breadboard, it gets slightly better but not a great deal. It seems the further away you move the positive and negative cables from the Pro Mini the worst things get e.g. reseting point being around the 20’s. I’ve also tried switching analog ins just in case I had a dry joint and tried another JST cable, etc.
The strange thing is that I’ve loaded the same sketch onto my Arduino Uno using a breadboard and its perfect.
My GUESS is that your FTDI adapter can’t supply the peak current needed by the Arduino plus the sensor. IOW you’re having brown outs. If the sensor is like this ;
… then the ave current is 33 to 50 mA. I don’t know the peak current but I’ll GUESS it’s something like 150 mA to get the range needed. Still not a lot but ???
The Sharp IR range finders do pull a lot of current when pulsing their IR LED. These typically require an additional 10uF (or greater), low ESR cap soldered right onto the Sharp’s connector pad. This ‘local’ cap supplies the current required to pulse the IR LED.
Will try tomorrow. I have got the pololu 5v step up/down converter I can try also. I will be using the sensor with an external 9v battery anyway, but wanted to see it working first within the serial monitor.
It’s just funny how the UNO works fine. I find most things seem to work better with the UNO but sadly there’s no room in my project box for one which is why I favour the pro mini or nano.