I bought a Sparkfun ultrasonic sensor yesterday to use with my Explorer HAT robot. I tested it on an Arduino UNO with this sample code from Sparkfun:
void setup()
{
// initialize serial communications
Serial.begin(9600);
}
void loop()
{
int sensor, inches, x;
// read the analog output of the EZ1 from analog input 0
sensor = analogRead(0);
// convert the sensor reading to inches
inches = sensor / 2;
// print out the decimal result
Serial.print(inches,DEC);
// print out a graphic representation of the result
Serial.print(" ");
for (x=0;x<(inches/5);x++)
{
Serial.print(".");
}
Serial.println("|");
// pause before taking the next reading
delay(100);
}
The sensor worked fine with the Arduino, outputting an accurate whole number. Then I connected it to the Explorer HAT Pro with 5V, GND, and Analog 1. I used this code:
import explorerhat as eh
import time
while True:
sensor = eh.analog.one.read()
inches = sensor / 2
print(inches)
time.sleep(0.1)
I am getting a different output with the Explorer HAT, a decimal number that is far different than the Arduino. Is my Python program no good, or does the Explorer HAT’s analog inputs operate differently than an Arduino’s?