-
Notifications
You must be signed in to change notification settings - Fork 0
temp reader for array (thermistor and arduino) #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @jana-daouk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new Arduino sketch that enables temperature measurement using a thermistor. It provides a foundational example for integrating analog temperature sensors with an Arduino board, performing necessary calculations to convert raw sensor data into human-readable temperature units, and outputting them via serial communication. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds an Arduino sketch for reading temperature from a thermistor. The code is a good starting point, but there are several areas for improvement. I've identified a critical issue regarding a potential division-by-zero error and an inconsistency between a comment and the code logic. I've also suggested several improvements for maintainability and readability, such as using constants for fixed values, scoping variables correctly, and a minor optimization for the temperature calculation. Addressing these points will make the code more robust and easier to maintain.
| // Assumes: 5V -> R1 -> A0 -> thermistor -> GND | ||
| R2 = R1 * (1023.0 / (float)Vo - 1.0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two issues here:
-
Inconsistent Comment: The comment on line 33 describes a circuit (
5V -> R1 -> A0 -> thermistor -> GND) that is inconsistent with the formula on line 34. The formulaR2 = R1 * (1023.0 / (float)Vo - 1.0)is for a circuit where the thermistor and fixed resistor are swapped (5V -> thermistor -> A0 -> R1 -> GND). This should be corrected to avoid confusion. -
Division by Zero: If
Vois 0 (which can happen if the thermistor circuit is open), the formula on line 34 will cause a division-by-zero error, leading toinfvalues in subsequent calculations. It's critical to handle this edge case to prevent undefined behavior and provide meaningful feedback.
Here is a suggested fix that corrects the comment and adds a safety check:
| // Assumes: 5V -> R1 -> A0 -> thermistor -> GND | |
| R2 = R1 * (1023.0 / (float)Vo - 1.0); | |
| // Assumes: 5V -> thermistor -> A0 -> R1 -> GND | |
| if (Vo < 1) { | |
| Serial.println("Error: Thermistor open circuit?"); | |
| delay(500); // Prevent spamming serial output | |
| return; // Skip the rest of the loop | |
| } | |
| R2 = R1 * (1023.0 / (float)Vo - 1.0); |
| @@ -0,0 +1,66 @@ | |||
| // Analog pin connected to the voltage divider midpoint | |||
| int ThermistorPin = 0; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| int Vo; | ||
|
|
||
| // Known fixed resistor value | ||
| float R1 = 10000; | ||
|
|
||
| // Variables for thermistor math | ||
| float logR2, R2; // thermistor resistance and its natural log | ||
| float T, Tc, Tf; // temperature in Kelvin, Celsius, Fahrenheit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variables Vo, logR2, R2, T, Tc, and Tf are declared as global but are only used within the loop() function. It's a good practice to limit the scope of variables to where they are needed. Please move their declarations inside the loop() function. This makes the code cleaner, easier to reason about, and prevents potential unintended side effects.
For example:
void loop() {
int Vo;
float logR2, R2;
float T, Tc, Tf;
// ... rest of the loop code
}| int Vo; | ||
|
|
||
| // Known fixed resistor value | ||
| float R1 = 10000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| float c1 = 1.009249522e-03; | ||
| float c2 = 2.378405444e-04; | ||
| float c3 = 2.019202697e-07; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Steinhart-Hart coefficients are constants and should be declared with const to prevent accidental modification at runtime.
| float c1 = 1.009249522e-03; | |
| float c2 = 2.378405444e-04; | |
| float c3 = 2.019202697e-07; | |
| const float c1 = 1.009249522e-03; | |
| const float c2 = 2.378405444e-04; | |
| const float c3 = 2.019202697e-07; |
| logR2 = log(R2); | ||
|
|
||
| // Apply Steinhart–Hart equation to get temperature in Kelvin | ||
| T = 1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The calculation of the Steinhart-Hart equation can be made slightly more efficient and arguably more readable by factoring out logR2, similar to using Horner's method for polynomial evaluation. This reduces the number of multiplications.
| T = 1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2); | |
| T = 1.0 / (c1 + logR2 * (c2 + c3 * logR2 * logR2)); |
No description provided.