Skip to content

Instantly share code, notes, and snippets.

@psychemist
Last active November 26, 2025 13:30
Show Gist options
  • Select an option

  • Save psychemist/5beca67083211345df71e8a9e75c876a to your computer and use it in GitHub Desktop.

Select an option

Save psychemist/5beca67083211345df71e8a9e75c876a to your computer and use it in GitHub Desktop.
Arduino Starter Project 3
// declare global constant variables
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup() {
// open a serial port connection between the Arduino board and computer
// allowing communication at 9600 bauds (bits per second)
Serial.begin(9600);
// assign digital pins as output and toggle off
for (int pinNumber = 2; pinNumber < 5; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop() {
// read analog input from sensor pin
int sensorVal = analogRead(sensorPin);
// print sensor info to computer's serial moitor
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
// convert analog reading ( 0 < x < 1023 ) to voltage ( MAX 5V )
float voltage = ( sensorVal / 1024.0 ) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
// convert voltage to temperature in Celsius
// -.5V for freezing temp offset at 0°C * 100 to scale (△10millivolts = △1C°)
float temperature = ( voltage - .5 ) * 100;
Serial.print(", Degrees C: ");
Serial.println(temperature);
// adjust LED light toggling based on baseline temperature
if (temperature < baselineTemp + 2) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if (temperature >= baselineTemp + 2 &&
temperature < baselineTemp + 4) {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if (temperature >= baselineTemp + 4 &&
temperature < baselineTemp + 6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} else if (temperature >= baselineTemp + 6) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment