Translate

Showing posts with label 4-20 mA. Show all posts
Showing posts with label 4-20 mA. Show all posts

Sunday, August 7, 2016

4-20 mA current output for Arduino Uno

The purpose of this project is to provide a 4-20 mA output from a PWM signal generated by a microcontroller ATmega328 and numerous other chips, such as the PIC. One of the more interesting applications of this circuit would be to replace or to realize a smart sensor with Arduino.

Last year I had designed a circuit suitable only for Arduino Due, this new work makes use of a common Arduino Uno or similar to create a standard 4-20 mA analog output.
Arduino Uno, or systems based on the ATmega328 chip has no a true analog output. The easiest way is to use one of the PWM outputs and filter the signal with a passive RC filter to obtain an analog signal proportional to the duration of the pulses. This expedient creates a considerable noise due to the frequency of the PWM itself. To eliminate the noise I used a second order active low-pass filter, Sallen-key type. The frequency of the Arduino PWM (with 16 MHz clock) on pin 9 is about 490 Hz, so I used a very low cutoff frequency (11 Hz) but with a bandwidth sufficient for the majority of industrial controls.

By connecting the filter directly to the PWM output is obtained a signal which varies from 0 to 5 V which would give an output current of 0 to 20 mA. The pulses duration is programmed with a word of 8 bits, losing 1/5 of the full scale. To improve the current resolution from 20/255 to 16/255, I modified the minimum amplitude of pulses from 0 to 1 volts, giving at the output a 4 to 20mA current. The block diagram is shown in figure 1.

Figure 2 shows the complete diagram of the circuit. To obtain pulses from 1 to 5 volts I had to use a 1 V source realized with U1A and the transistor Q1 that works as a switch. The operational U1B operates as a separator; the filter uses U1C and the voltage / current converter uses U1D and Q2.
The transistor Q1 inverts the PWM signal, so the software must complement the number of PWM duty cycle.

The trimmer pot Rp1 is used to adjust the minimum output current (4 mA) and the Rp2 to adjust the maximum (20 mA). The theoretical value of the emitter resistor is Re = 5/0.02 = 250 W, but that does not take into account the tolerances of the voltage supply of Arduino and of the resistors.
The resistor R8 is used as U1D output current limiter in the situation of absence of load.
A step down converter is a good solution for powering the system because of the 24 V, this value can be varied from 12 to 30 V, depending of the load circuit.
Arduino Uno has a +5 V output pin, It does not recommend using it as a power input          inasmuch this would be in parallel with the internal regulator but it can be powered at +5V using the USB connector, other boards as Arduino Pro Mini, have a +5 V input.

Hardware components
1x Arduino board,
1x Step-down switching converter,
Components list
R1= 27 kW ±5%
R2= 47 kW ±5%
R3= 10 kW ±5%
R4= 27 kW ±1% metal film
R5= 6.2 kW ±1% metal film
R6= 100 kW ±1% metal film
R7= 100 kW ±1% metal film
R8= 1 kW ±5%
R9= 270 W ±1% metal film
R10= 1.8 kW ±1% metal film
Rp1= 1 kW trimmer
Rp2= 10 kW trimmer
C1= 100nF Mylar
C2= 100 nF Mylar
C3= 200 nF Mylar
C4 = 10 MF,50V Electrolytic
C5 = 100 nF Mylar
U1= LM324 quad op amp
Q1 = 2N3904 or eq.
Q2= 2N2219A or eq.
The capacitors used for the filter must be measured with a capacimeter, for my prototype I selected for C3 some 220 nF capacitors to search for a value that approached 200 nF and C2 have selected a value half of C3.  Q1 is a transistor that must have a low Vce(sat.) and Q2 must have a current gain of at least 100 and a Vceo of at least 40V with a minimum power of 500mW.
The operational amplifier U1 must be suitable also for single-rail power supply, such as LM324.
The components layout of my prototype is shown in Figure 3, the resistor on the top is a precision load used for calibration of the system. Q2 has a small heat sink because, with at 20 mA and a low voltage load,  as in this case,  dissipates: (24-3-5) *0.02 = 320 mW. In these circumstances is better to reduce the 24 v.
The test program
To test the system I used an Arduino with an LCD display and a potentiometer connected to analog input A0, as pin PWM I used D9. The program is very simple: read the potentiometer, converts 10 to 8-bit Analog reading and produces the PWM.

// program to test Arduino Uno PWM
// G. Carrera 2 ago 2016

#include <LiquidCrystal.h>

int PWMpin = 9;      // PWM out on digital pin 9
int analogPin = 0;   // potentiometer connected toA0
int val = 0;         // variable to store the read value
char spacestring[17] ="                ";

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  pinMode(PWMpin, OUTPUT); // sets the pin as output
  lcd.begin(16, 2);// set up number of columns and rows
  lcd.setCursor(0, 0);// set the cursor to column 0, line 0
  lcd.print("Stalker PWM test");// Print a message to the LCD
}

void loop() {
  val = analogRead(analogPin) >> 2;// 10 to 8 bit conversion 
  analogWrite(PWMpin, val);
  lcd.setCursor(0, 1);
  lcd.print(spacestring);
  lcd.setCursor(0, 1);
  lcd.print(val);
  delay(500);
}

I reported on the spreadsheet the PWM values and the measurements in volts made on a precision resistor (150 W ±0.5%) that worked as a load,. The PWM / output current diagram is shown in Figure 4.
The linearity is very good as confirmed by the coefficient of determination R2 = 0.999992.
If you want a positive slope, the value must be complemented to 255 in this mode:
val = 255-val;
In my program, you could generate a new value every 500 milliseconds (2 Hz), but you could reduce this period of up to 100 ms (10 Hz).



Monday, August 17, 2015

4-20 mA current output for Arduino Due

Arduino Due does not have an analog output voltage from 0 V to Vref, but from 1/6 to 5/6 of the reference voltage, corresponding to voltage values of 0.55 V and 2.75V with a typical Vref = 3.3 V. This is also confirmed by the Atmel (see bibliography 1).
I do not know if it is wanted by the ARM Cortex-M3 CPU designers, but the ratio between the maximum value and the minimum of the output voltages of Arduino Due is exactly five, such as that between 20 and 4 mA, the standard used for the transmission of analog measurements in the industrial plants.

This has facilitated the design of an electronic circuit in order to obtain at the output a current range of 4 to 20 mA. The following diagram shows this simple circuit. It uses a single-rail LM358 operational amplifier and an NPN darlington transistor and a few other components.
The operational amplifier U1 and the transistor Q1 realize a tracking system, so that the voltage on emitter resistor R2 is equal to the input voltage.  Keeping constant the voltage on a resistor, by the Ohm's law, means that even the current is constant. In this way, the transistor emitter current Ie, flowing in R2, is dependent on the input voltage, therefore the circuit behaves as a current generator controlled by the input voltage.
From Kirchhoff's Current Law the collector current is: Ic = Ie – Ib, where Ib is the base current of transistor: Ib = Ic /hfe and hfe is the common-emitter current gain. Using a transistor with a hfe> 100 or greater, we can neglect the base current of the transistor and assert that also the collector current remains constant and proportional to the input voltage. A darlington transistor has an even greater current gain, so that we can assume that Ic = Ie.
The trimmer pot is used to adjust the maximum output current (20 mA) at the maximum number (4095) of the DAC or the maximum output voltage. This value, in the typical case of 3.3*5/6 is equal to 2.75 V, for which:
R2 = 2.75V /20mA = 137.5 Ω
In practice there may be slightly different values, therefore, it's better to use a potentiometer to precisely calibrate the current.
Powering the circuit with 5 V, supplied by of Arduino, the load can’t have higher voltages of 1 volt. Indeed, at 20 mA the voltage on R2 will be equal to about 2.75 V, which must be added to the Vce of the transistor which must not saturate. During the tests I used a digital ammeter that require a maximum of 0.2V on its terminals. For these reasons it is better to use higher supply voltages as 12 V or, better, 24 V. By varying the supply voltage does not involve variations of the circuit, but only an increase of dissipation on the transistor. In these cases a darlington medium power transistor is a good choice, as a TIP110, BD675, or similar.
 Don’t use operational amplifier as LM741, LM1458, TL081 and other that are not suitable for single-supply.
The photo below show the circuit, built on a bread board, during the test. For the interface with my Arduino Due system, I used a DB25 connector, mounted under the card.
To test the circuit, I wrote a simple program that sends to the DAC0  of Arduino Due, values of N according to the table: {0,512,1024,2048,3072,3584,4095}. They are generated in succession, each time I press the a button connected to pin 32. In the picture below you can see the circuit connected to my system, based on an Arduino Due and a LCD display. 
For the calibration measurements I used both a digital DMM, as shown in the photo, and a more accurate high resolution voltmeter on a precision 200.0 Ohm resistor, acting as load.
The results of these measurements were very good, as is also clear from the trend curve shown in the following figure and from R2, almost equal to one.
References
1)      “Atmel ARM Cortex-M3 Product Family (SAM3)”, Atmel application note 42187A−SAM−10/2013