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
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
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
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.