We know that, we need calibration factors (scale factors) to read current sensor data (ACS758) to arduino. I am also using voltage reader using voltage divider circuit for my project. I need to build 5 such boards and need to change calibration factor in main program everytime due to tolerance of componenets. Currently for one of my board reference_factor is 0.81 and this will change due to resistor tolerance for other boards.

Because of this issue I need to maintain 5 different codes for 5 different boards. These sensor values controls on and off of certain relays. (Not shown in the code).

can I load this referrence_factor externally through text file or as any other medium so that I need not change the code everytime.? I am using arduino Nano.

int reference_Factor = 0.81;

void setup() {
  // put your setup code here, to run once:

void loop() {
  // read the raw data coming in on analog pin 0:
  int lightLevel = analogRead(A0);
  // Convert the raw data value (0 - 1023) to voltage (0.0V - 5.0V):
  float voltage = lightLevel * reference_factor;
  // write the voltage value to the serial monitor:
  • 2
    You can store it in EEPROM. Write a method in setup that will see if EEPROM has been written and if it hasn't then it will let you input a number to use. You set it up on the first powerup at the factory. From there on out it sees that EEPROM has been written and skips that bit and instead reads the factor out of EEPROM.
    – Delta_G
    Apr 24, 2020 at 5:39
  • By the way, your current code is flawed. You should look up what types of values an int variable can hold.
    – Delta_G
    Apr 24, 2020 at 5:40
  • Thanks for your reponse. I have indeed used float in my actual code. Apr 24, 2020 at 9:52
  • You should always post the "actual code" when asking for help. Otherwise you end up getting answers that don't apply to your "actual code" and then you've wasted both our time and yours.
    – Delta_G
    Apr 24, 2020 at 15:26

1 Answer 1


As written in the comments you coulduse eepron, but in your scenario it has one big disadvantage: Everything has to be written at compile time (aka when you programm the Nano, you have also to fill the eeprom with the relevant values), so e.g. if you have a temperature depending factor you want to change while on the field...
I would go for a SD-card reader:

  • Cheap to get (2$)
  • Easy to handle in code (read from a file is a standard function)
  • using a PC compatible file systen FAT enables reading/writing onto the SD card on a PC/other device without recompiling your code.
  • If you switch sensors for whatever reason, just write a new scale.txt file to the SD-card, insert and reboot the nano to get the new values
  • you could still use eeprom as a fallback scenario

As a tip (also mentioned in the comments by Delta_G) int for something like 0.01 is not what you want - define as float and watch out for the "conversion trap" when using with other variables not defined as float.

  • Thanks for your respnse. As I have not attached complete code here, int is a typo error. I do undertsnd the data structure. I have indeed used float in my code. I wish to know whats the difference here in directly uploading code and uploading code using EPROM? I need to edit in the main code for all 5 boards even if I will be using EPROM right? Apr 24, 2020 at 9:51
  • Yes either loading the eeprom from the program (5 versions) or via an extra program you flash before loading (5 flash eeprom versions + 1 identical program) or some kind of input (button presses/led blink to enter during first start - 1 program). Thats why I proposed SD as hassle free ;-) Apr 24, 2020 at 12:23
  • No you wouldn't have to load the EEPROM at compile time. You could compile the same code for all boards. What you would need is some code in setup that check if a value has already been set in EEPROM. If it hasn't then it calls a function that asks you over the serial monitor and lets you enter the number and stores it in EEPROM. This code would only run the very first time the board is turned on. After that nobody will ever see that prompt unless you blank the board and reload.
    – Delta_G
    Apr 24, 2020 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.