Tuesday, January 31, 2023

Very fast Analog data logging using Arduino

For fast analog data acquisition on an Arduino UNO, you can utilize the analogRead() function, but to optimize it for speed, we can:

1. Use Direct Register Manipulation to bypass the overhead of the `analogRead()` function.
2. Disable ADC interrupts if you're not using them.
3. Set the ADC prescaler to a value that allows faster conversions.

Here’s a code example that shows how to configure the Arduino UNO to acquire analog data very fast:

Fast Analog Data Acquisition Code


const int analogPin = A0;  // Analog input pin
const int numSamples = 1000; // Number of samples to acquire

// Variables to hold ADC results
volatile uint16_t analogData[numSamples];
volatile unsigned long lastMicros = 0;
volatile int sampleIndex = 0;

void setup() {
  Serial.begin(115200);  // Initialize Serial for output
  // Configure the ADC for fast conversion
  ADMUX = (1 << MUX0);    // Select A0 pin (MUX0 = 1)
  ADCSRA |= (1 << ADEN);  // Enable ADC
  ADCSRA &= ~(1 << ADIF); // Clear ADC interrupt flag
  ADCSRA |= (1 << ADATE); // Enable Auto Trigger
  ADCSRA |= (1 << ADPS2) | (1 << ADPS1);  // Set prescaler to 64 (fastest)
  ADCSRA |= (1 << ADSC);  // Start the conversion
  ADCSRA |= (1 << ADIE);  // Enable ADC interrupt

  // Configure the ADC to be in free-running mode
  ADCSRA |= (1 << ADATE); // Enable auto-triggering mode (free running)
  ADCSRB = 0;             // Free running mode

  sei();  // Enable global interrupts
}

void loop() {
  // We acquire data until numSamples is filled
  if (sampleIndex >= numSamples) {
    // Send the data to Serial when done
    for (int i = 0; i < numSamples; i++) {
      Serial.println(analogData[i]);
    }
    sampleIndex = 0;  // Reset for the next acquisition
  }
}

// ADC Conversion Complete ISR
ISR(ADC_vect) {
  if (sampleIndex < numSamples) {
    analogData[sampleIndex++] = ADC;  // Store the ADC result
  }
}


Key Optimizations:

1. Direct Register Manipulation:
   - Instead of using `analogRead()`, this code reads directly from the ADC register (`ADC`).
   - It sets the ADC prescaler to 64 for faster conversion, and uses `ADCSRA` for controlling ADC features.

2. Auto-trigger (Free Running Mode):
   - The ADC is set to continuous conversion mode, so it automatically triggers a new conversion after each one. This eliminates the need for manual triggering.

3. Interrupts for Fast Acquisition:
   - The `ADC_vect` interrupt is triggered every time an analog-to-digital conversion is complete, enabling fast acquisition without delay.

Prescaler Settings for ADC Clock:
The ADC clock in an Arduino UNO can be controlled using the prescaler, which divides the 16 MHz system clock to give the ADC a lower clock frequency. The prescaler is set in the `ADCSRA` register:

- Prescaler of 16 (default): 1 MHz
- Prescaler of 64: 250 kHz (faster, but higher noise)
- Prescaler of 128: 125 kHz (useful for higher precision but slower)

In the example, we used a prescaler of 64, which provides a balance between speed and accuracy.

Data Output:
- The `analogData` array will store all the ADC readings, and once the acquisition of the desired number of samples (`numSamples`) is complete, it sends the data over serial to the PC for analysis.

Notes:
- The code is designed for fast analog data acquisition. You can adjust the `numSamples` for longer or shorter acquisition.
- The resolution of the analog readings will be 10 bits (0-1023) as provided by the standard ADC in Arduino UNO.
- If you need higher speeds, consider moving to a more advanced board, such as Arduino Due or Teensy, which support faster ADCs.


Sunday, May 22, 2022

Synchronous pulse train generation using Arduino

Using arduino, i have generated synchronized pulses with a fixed delay between them. The parameters can be remotely adjusted using serial port.

The pulse 2 (Blue) goes high after some user settable delay of Pulse 1(Yellow) rising edge.


 If we set td=tm, we can get consecutive pulses as below:



Monday, March 14, 2022

Analog signal isolation using HCNR200/201 optocoupler

 

The HCNR200/201 is a high-linearity analog optocoupler which can be used to isolate analog signals. It offers good stability, linearity, bandwidth, simple design and low cost. With choice of suitable application circuit it can also incorporate amplification, attenuation, offset, inversion among others to the input signal. 

 Details of the IC along with datasheet can be found in the link below.

https://www.broadcom.com/products/optocouplers/industrial-plastic/specific-function/high-linearity-analog/hcnr200

 

 At first glance, understanding the application circuit is difficult for a new user. 

To get complete isolation, make sure that grounds as well as bias supplies of input and output stage are isolated. Note the label used in above circuit for input stage are GND, VCC, VEE and that of output stage are GND2, VCC2 and VEE2. I generally use two isolated DC to DC converters each powering one stage at a time.

Now coming back to working of the circuit. Lets us analyze the input stage first.

The input side op-amp always tries to force same input voltages at its two input terminals in close loop connection. Thus, the input side  photodiode PD1 (terminal 3,4 of HCNR200) will have zero voltage across it. A positive voltage at inverting terminal of U1 will swing the output to negative rail causing current flow through LED (terminal 1,2 of HCNR200). Also the positive voltage will cause a current through R1 which will eventually flow through photodiode PD1.

IPD1 = Vin/ R1

Current is linearly related to input voltage. 

Since photodiode PD1 and PD2 are identical to each other, IPD2 should be equal to IPD1 ideally. Practically the relation is

 IPD2 = K x IPD1

where K is gain coefficient.

Output voltage can be given as Vout =  R2 x IPD2


Wednesday, February 16, 2022

Differential, Referenced single ended (RSE), Non-referenced single ended (NRSE) signals interfacing with DAQ

Before acquiring data using the DAQ board the following points must be considered

The nature of the signal source(grounded or floating)
The grounding configuration of the amplifier on the DAQ board
Finding a Common Ground 
Earth ground - potential of the earth below your feet.
         Most electrical outlets have a prong that connects to the earth ground which is usually wired into the building electrical system for safety. Many instruments are also” grounded” to this earth ground System ground. This type of grounding is for safety.
         Ground Symbol 
 
        Referenced Signal

        Reference ground -return path or signal common.This is usually the reference potential. The common ground may or may not be wired to the earth ground.

         Many instruments ,devices and signal sources provide a reference (the negative terminal,common terminal  etc that gives meaning to the voltages that we are measuring.

         Signal Source Reference Configuration
         Signal sources come in two forms-(1) Referenced sources called grounded signals, (2) Non referenced sources called floating signals

        Grounded signal sources have voltage signals referenced to a system ground, such as earth or a building ground.

 

        Devices that plug into the building ground through wall outlets such as signal generators and power supplies are examples of Grounded signal sources.

         Floating signal sources contain a signal that is not connected to an absolute reference such as earth or a building ground. Batteries, Thermocouple, transformers are examples of floating signal sources.

 

         Differential Connections (DIFF configuration):
        Differential connections are those in which each  analog input signal has its own reference signal or signal return path. Each input signal is tied to the positive input of the instrumentation amplifier, and its reference signal, or return, is tied to the negative input of the instrumentation amplifier.
        When configuring the DAQ for DIFF input, each signal uses two of the multiplexer inputs-one for the signal and one for its reference signal.
         Therefore, only half the analog input channels are available when using the DIFF configuration.  
         Conditions for using DIFF configuration:
         Use the DIFF input configuration when your DAQ system has any of the following conditions: 
  •             Input signal levels are low (less than 1V)
  •             Leads connecting the signals to DAQ system are at long distance.
  •             Any of the input signals require a separate ground-reference point or signal-return.
  •             The signal travels through a noisy environment.
  •  
A differential instrument requires two inputs where neither input to the instrumentation amplifier is referenced to a system ground. For example, CH0+ and CH0- are wired into the positive and negative terminals of the instrumentation amplifier respectively, but they are not connected to the measurement system ground (AI GND).
       The differential voltage across the circuit pair is the desired signal, yet an unwanted signal that is common to both sides of a differential circuit pair can exist. This voltage is known as common-mode voltage. An ideal differential measurement system completely rejects, instead of measures, the common-mode voltage for more accurate measurements. Practical devices, however, have limitations described by specifications such as common-mode voltage range and common-mode rejection ratio (CMRR).
          Differential input connection for Grounded sources
 
Differential input connection for floating sources
 
 
RSE and NRSE connections
     RSE measurement system is used to measure a floating signal, because it grounds the signal with         respect to building ground.
 
     NRSE measurement system, all measurements are made with respect to a common reference, because all of the input signals are already grounded.
     Use the RSE configuration for floating signal sources; in this case, the DAQ boards provide the     reference ground point for the external signal.  
        Use the NRSE configuration for ground-referenced signal sources; in this case, the external signal     supplies its own reference ground point and the DAQ boards should not supply one.
 

Temperature sensors- a comparative analysis

Temperature sensors are widely used in various applications, including industrial, automotive, medical, and consumer electronics. The most p...