Dear Paul,
thank you for your reply. The problem does seem to be in the hot swapping. I have used an external power supply for the sensor now and swapping or disconnecting wires is no problem now. It seems connecting / disconnecting Vcc / gnd from the arduino is a problem with this sensor.
That said, I have used arduinos for a couple of years now and have not encountered this problem before. I *will* remember your advice for when I run into problems like this one again, I'm amazed it never went wrong before. I wonder if it is specific for this sensor (it draws about 0.01 Amps).
The sensor is working now as expected, more or less. It measures about 40 measurements, waits for a while, then starts measuring again. This is not the expected behaviour, but it repeats it over time: a delay between series of measurements where it is not programmed into the code:
#define ECHOPIN 2 // Pin to receive echo pulse
#define TRIGPIN 3 // Pin to send trigger pulse
void setup(){
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
void loop(){
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH); // Read in times pulse
distance= distance/58; // Calculate distance from time of pulse
Serial.println(distance);
delay(50); // Wait 50mS before next ranging
}
I might post this question under sensors, because I have the feeling this is not caused by the connection with the PC.
Cheers
Jack