LED refractometry with Arduino

Hello everyone,
I'm an undergrad in chemistry and I love programming and building stuff with Arduino. I don't have an extensive knowledge in electronics but I know the basic stuff.
That said I also love beer and recently I started brewing my own beer. Since I wanted to keep track of the fermentation process, I made my own DB and API to host the measurements sent by a temperature sensor. The temperature is a crucial parameter to make the fermentation go the right way, but the wort gravity is also of interest to measure the rate at which the yeast is transforming the sugars, so I thought of different ways I could build a hydrometer.

The simplest one is by measuring the weight of a fixed volume container, but it's hard to have an accurate weight reading, and it would be difficult to automate the process. Another one is through two pressure sensors, by measuring the differential pressure across a fixed height and with that the density of the fluid. I couldn't come up with a cheap pressure sensor with a good precision, though.
The last one involves optics. Since the wort gravity is related to the refractive index I could "simply" measure the refractive index of a sample collected through an electrovalve or a pump (and then discarded for hygienic reasons), then obtain the density value through interpolation with a curve built with known-density samples.

To make this work I would need a laser, a prism directly interfaced with the sample, and a sensor from which I would read the position of the light beam. I attached a quick sketch of what I have in mind.

I'm not sure about one thing though. I used the same positioning I've seen in a website (I can't find the link anymore)

EDIT - I found the website explaining the principle used for the measurement: http://contrologic.co.th/web/refractometer-digital-measurement-principle/
It's based on the critical angle - the angle at which the refracted beam is parallel to the surface - which unfortunately makes the laser a bad choice. But by replacing it with a LED it should work, right? And the CCD should look for the position of the shadow which is a consequence of the light being refracted.

Does anyone know if it works this way? And for the CCD sensor I thought I could buy a bar code reader - since it's basically doing the same thing, reading the position of bars. Is it a good choice, given I would also need to find a way to interface it with arduino?

Thanks in advance :slight_smile:
Paolo

This salt water refractometer works on exactly that principle. It uses a linear CCD to measure the shadow edge. It works with just one drop of liquid and is remarkably accurate. The light source is a yellow LED.

You may be able to use it out of the box, by fitting a curve to the numbers it produces for known concentrations of wort.

The PIC chip on board would be difficult to reprogram, but the device is easy to take apart and the CCD board can be connected directly to a Teensy 3.2, as I did a while back.

Thank you so much for your answer! I updated the sketch to reflect how I believe it should work, I hope I got it right.

The refractometer in your link would be perfect to get half of the work done, but unfortunately in my country I can only get it for around 180€.
A barcode reader can be as cheap as 20€. Theoretically it should rely on a linear CCD, I don't think that cheap scanners have a camera and the hardware needed to process the image data...

1 Like

Theoretically it should rely on a linear CCD

Possibly, but I've taken apart a bar code scanner that uses a single photodiode. CCDs can be difficult to interface, and perhaps impossible if you can't find a data sheet.

The TCD1304DG linear CCD in the linked refractometer does have a data sheet, and is easy to interface to Arduino. You can buy that CCD alone, but you then have to build the interface and the prism part.

Are many weeks to months of your effort cheaper than 180€?

jremington:
The CCD in the linked refractometer does have a data sheet, and is very easy to interface to Arduino. You can buy that CCD alone, but you then have to build the interface and the prism part.

I totally forgot I could search for the piece alone...
I found it for around 5€ from china but given the current situation I guess it takes months to deliver. From the UE it's just 20€, however.
I know that the full refractometer would be great but in addition to the price tag there's the risk that I make a mess and in that case I've lost the full 180€ price :sweat_smile:

You said you interfaced it with a teensy, I don't have one though... Can it be interfaced to an Arduino or possibly a nodeMCU without additional circuitry, or should I get a teensy?

Thank you again

1 Like

You need a fast MCU, because you have to generate a sample clock frequency between 800 kHz and 4 MHz and sample the data at that frequency using an ADC.

The Teensy also has enough memory to store the entire 3648 element data array at once, although that is not required for detecting a shadow.

The Teensy I used runs at 96 MHz, and cost about $20. To build your own CCD interface, you might be able to buy a PCB from the project initiator, discussed in the link posted in reply #3.

The nodeMCU I have is based on the ESP8266 which should run at 80 MHz, according to wikipedia. I don't know if that's enough, IIRC to sample a 4 MHz signal I need a rate of at least 8 MHz, but I don't know if other factors that limit the speed come into play. As for the memory it should have 128 kB, considering 4 bytes per data point (total 14 kB) it should be enough. The only drawback is that it has only one analog input.

I'll give a look at the link, thanks

You need only one analog input, but the ADC must be fast (sample frequency at least 1 MHz).

jremington:
You need only one analog input, but the ADC must be fast (sample frequency at least 1 MHz).

Hello again,
I ran this test code on the esp8622

  ...
  // Default analogRead
  unsigned long times[500];
  unsigned long beginTime, endTime;
  int e;

  for(int i = 0; i < 500; i++) {
    beginTime = micros();
    e = analogRead(A0);
    endTime = micros();
    
    times[i] = endTime - beginTime;
  }
  ...
  uint16_t measurements[MEASUREMENTS_COUNT];

  // Fast ADC readings
  beginTime = micros();
  system_adc_read_fast(measurements, MEASUREMENTS_COUNT, 8);
  endTime = micros();

and I got a sample rate of 10 kHz, 12 kHz if I double the cpu frequency to 160 MHz, 28 kHz if I disable the WiFi chip, 200 kHz if I use system_adc_read_fast + all the previous. I'm far from the requirements, and even trying to further optimize it I doubt it will ever reach the 1 MHz rate.

I will get a teensy, probably the 4.1 since its price is the same as the 3.2, will it work? I can even get the nucleo stm-32 for the same price but I guess it relies on its own development environment so I'll stick with the teensy.

As for the circuitry involved, I've seen there are two different PCBs, one with a transistor and other electrical components, and another one that apparently has none of them... do I really need the additional circuitry?

Thank you for the help

Any reasonable fast MCU should work, as long as the timing restrictions in CCD data sheet can be met.

As for the PCB, the manufacturer recommends a circuit (again, in the CCD data sheet) with components that serve as buffers. If you ignore that advice, you are on your own.

I was lucky enough that the project maintainer at the link you provided could ship me one of his PCBs. I also ordered a Nucleo board, the same he uses in his project, as I found it for a very cheap price and there's already some code I can start with at the project site.

I hope to come back to this post with a story of success, in the meantime thank you very much for the precious info you gave me!

It is usually a good idea not to reinvent the wheel. I'll look forward to reading your success story!