Skip to content

Instantly share code, notes, and snippets.

@phodal
Created February 13, 2014 12:10
Show Gist options
  • Save phodal/8973993 to your computer and use it in GitHub Desktop.
Save phodal/8973993 to your computer and use it in GitHub Desktop.
arduino with hc-sr04 lm35
const int trigPin = 13;
const int echoPin = 12;
float temp;
int tempPin = 0;
void setup() {
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
temp = analogRead(tempPin);
temp = temp * 0.48828125;
Serial.println(temp);
delay(1000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment