SDlookup is an Arduino/ESP32 library for temperature sensor simulation using resistance lookup tables. It provides functionality to read temperature-resistance pairs from an SD card and calculate corresponding voltage outputs based on a voltage divider configuration.
- Read temperature-resistance lookup tables from SD card
- Calculate voltage outputs for temperature inputs
- Support for custom SPI pin configurations
- Range checking for temperature inputs
- Configurable step size for temperature values
- Download the library files or Include as libdep in platformio.ini
- Place them in your Arduino libraries folder
- Include the library in your sketch:
#include "SDlookup.h"
- Arduino.h
- SD.h
- SPI.h
The library requires:
- An SD card module connected via SPI
- Proper voltage (3.3V or 5V depending on your module)
- Correct pin connections:
- MOSI (Master Out Slave In)
- MISO (Master In Slave Out)
- SCK (Serial Clock)
- CS (Chip Select)
maxReading
: Maximum number of readings that can be stored (default: 400)
The file must use semicolons (;) as separators, not commas.
The file must contain:
- First Line: Contains two values:
- First value: Step size for temperature increments
- Second value: Maximum temperature of the range
- Second Line: Contains two values:
- First value: Minimum temperature of the range
- Second value: Initial resistance value
- Subsequent Lines: Temperature-resistance pairs:
- First value: Temperature
- Second value: Corresponding resistance
1.0;100.0 # Step size;Maximum temperature
-40.0;5000.0 # Minimum temperature;Initial resistance
-39.0;4800.5 # Temperature;Resistance pairs
-38.0;4600.3
-37.0;4400.1
...
- Each line must contain exactly one semicolon
- All values must be valid floating-point numbers
- Maximum of 400 lines (defined by maxReading)
- Values should be in simple decimal format (e.g., "25.0" not "2.5e1")
- No headers or comments are allowed in the actual file
- No empty lines are allowed
- No whitespace before or after values is necessary
Initialize the SD card with custom SPI pins.
bool initSD(uint8_t MOSI, uint8_t MISO, uint8_t SCK, uint8_t CS)
MOSI
: Master Out Slave In pin numberMISO
: Master In Slave Out pin numberSCK
: Serial Clock pin numberCS
: Chip Select pin number
bool
: Returns 0 if initialization is successful (Note: This appears to be inverse logic)
Reads temperature and resistance values from a CSV file on the SD card.
bool readSDarray(float *arrayT, float *arrayR, String filename)
arrayT
: Pointer to temperature arrayarrayR
: Pointer to resistance arrayfilename
: Name of the CSV file to read
bool
: Returns 1 if successful, 0 if failed
Converts temperature to voltage using lookup tables and voltage divider calculations.
float tempToU(float tempr, float *Rarr, float *Tarr, float *dspRes, float *dspTemp)
tempr
: Input temperature valueRarr
: Pointer to resistance array (includes step size and max temp)Tarr
: Pointer to temperature array (includes min temp)dspRes
: Pointer to store calculated resistancedspTemp
: Pointer to store calculated temperature
float
: Returns 1 if successful, 0 if temperature is out of range
Maps a float value from one range to another.
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
x
: Value to mapin_min
: Input range minimumin_max
: Input range maximumout_min
: Output range minimumout_max
: Output range maximum
float
: Mapped value
#include "SDlookup.h"
// Pin definitions
#define MOSI_PIN 23
#define MISO_PIN 19
#define SCK_PIN 18
#define CS_PIN 5
float tempArray[maxReading];
float resArray[maxReading];
float resistance, voltage;
void setup() {
Serial.begin(115200);
// Initialize SD card
if(initSD(MOSI_PIN, MISO_PIN, SCK_PIN, CS_PIN)) {
Serial.println("SD initialization failed");
return;
}
// Read lookup table
if(!readSDarray(tempArray, resArray, "temp_lookup.csv")) {
Serial.println("Failed to read lookup table");
return;
}
// Print configuration
Serial.printf("Step size: %.1f\n", resArray[0]);
Serial.printf("Temperature range: %.1f to %.1f\n", tempArray[1], resArray[1]);
// Convert temperature to voltage
if(tempToU(25.0, resArray, tempArray, &resistance, &voltage)) {
Serial.printf("For 25°C: Resistance=%.2f, Voltage=%.2fV\n",
resistance, voltage);
} else {
Serial.println("Temperature out of range!");
}
}
void loop() {
// Your code here
}
- Check if the SD card is properly formatted (FAT16/FAT32)
- Verify the SPI connections
- Ensure the CS pin matches your configuration
- Check if the SD card is properly inserted
- Verify the file name (8.3 format recommended)
- Check the file format matches the specification
- Ensure no empty lines or extra characters in the file
- Verify all values are valid floating-point numbers
- Check if the temperature is within the defined range
- Verify the lookup table contains appropriate values
- Ensure the step size matches your requirements
- Maximum of 400 readings can be stored (defined by maxReading)
- File names must be compatible with SD FAT filesystem
- Values must be within the float range of the platform
- Temperature must be within the range defined in the lookup table
Improvements to the library are welcome. Please follow these steps:
- Fork the repository
- Create your feature branch
- Submit a pull request
This library is released under the MIT License.
- 1.0.0: Initial release
[Your Name/Organization]