RPi and 4-20mA Outputs

Here is a code example using the Pi-SPi-2AO dual channel analog output module.

Pi-SPi-2AO Raspberry Pi 4-20 mA Output Module

The module uses a dual 12 bit DAC, the MCP4922. Each mA output has a mirror VDC output. The output circuit requires an external supply voltage, I am using a wall adapter style 24 VDC power supply. Each output has a signal strength LED, in this example mA Output 1 is set to 4 mA and mA Output 2 is set to 20 mA.

Schematics can be found on the product page Pi-SPI-2AO link.

The outputs are very easily defined as follows:

Reference Vref = 3.3. The mA circuit is optimized to give 20 mA at a 3VDC output from the DAC. Therefore, 20 mA = 3/3.3 * 4096 DA Counts = 3723 DA Counts.

Here is a very simple test routine written in C (using the Geany compiler on the RPi 3)

/*
 * pispi_2ao.c
 *
 * Copyright 2016  
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <math.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>

// Define Pins

#define PIN_EN_DA  22
    
// Prototypes    
void Initialize_Pi_Hardware(void);
void Update_Analog_Output(unsigned int dac1, unsigned int dac2);
     
// Variables
unsigned int DAC1_Counts;
unsigned int DAC2_Counts;

// main
int main(void) {
    
      wiringPiSetupGpio();           
      Initialize_Pi_Hardware();      
      digitalWrite(PIN_EN_DA, HIGH);
              
    while(1)
    {
                        
//        DAC1_Counts = 0;    // 0 = 0 mA
        DAC1_Counts = 745;    // 745 = 4 mA    
        DAC2_Counts = 3723;    // 3VDC/3.3Vref * 4096 = 3723 = 20 mA
        
        printf("DAC1 AD Counts Output  = %d \n", DAC1_Counts);    
        printf("DAC2 AD Counts Output  = %d \n\n", DAC2_Counts);    

        Update_Analog_Output(DAC1_Counts, DAC2_Counts);        
        delay(500);                
    }

    return 0;
}

void Update_Analog_Output(unsigned int dac1, unsigned int dac2) {

    unsigned int  output;
    unsigned char buf[2];
    
    wiringPiSPISetup(1, 100000);
    
    // Output DAC 1
    output = 0x3000;
    output |= dac1;
    buf[0] = (output >> 8) & 0xff;
    buf[1] = output & 0xff;
    digitalWrite(PIN_EN_DA, LOW);    
    wiringPiSPIDataRW(1,buf,2);
    digitalWrite(PIN_EN_DA, HIGH);
    delay(500);
    
    // Output DAC 2
    output = 0xb000;
    output |= dac2;
    buf[0] = (output >> 8) & 0xff;
    buf[1] = output & 0xff;
    digitalWrite(PIN_EN_DA, LOW);
    wiringPiSPIDataRW(1,buf,2);
    digitalWrite(PIN_EN_DA, HIGH);
}

void Initialize_Pi_Hardware(void) {
    
    // SPI CS Enable Lines
    pinMode(PIN_EN_DA, OUTPUT);
}

 

SaveSaveSaveSave

Leave a comment

Please note, comments must be approved before they are published