Introduction: Arduino, Thumbwheel Switch and MCP4821, DC Millivolt Source

Picture of Arduino, Thumbwheel Switch and MCP4821, DC Millivolt Source

Abstract

Millivolt sources are mainly used for simulation / calibration / testing purpose. Also it will be used for simulating the thermocouple voltage and feeding input for analog recorders. In this article, an Arduino and 12 bit resolution, single channel DAC (MCP4821) is used for generating the millivolt. This design, will generate millivolts between 0mV to 4095mV at 1mV accuracy / step. User can select the millivolt by using the thumbwheel switch, connected at Arduino digital IO pins.

Parts and components

  • Arduino Uno board
  • Thumbwheel switch = 4 Nos
  • IN4148 Diode = 16 Nos
  • 10K Resistor = 4 Nos
  • MCP4821 DAC = 1
  • 330R resistor = 1 No
  • RED LED = 1 No

Step 1: Schematic

Picture of Schematic
  • The MCP4821 is a single channel, 12-bit buffered voltage output Digital-to-Analog Converters (DACs).
  • The devices operate from a single 2.7V to 5.5V supply with an SPI compatible Serial Peripheral Interface.
  • The devices have a high precision internal voltage reference (VREF = 2.048V).
  • The user can configure the full-scale range of the device to be 2.048V or 4.096V by setting the Gain Selection Option bit (gain of 1 of 2).
  • The MCP4821 provide high accuracy and low noise performance for consumer and industrial applications where calibration or compensation of signals (such as temperature, pressure and humidity) are required.
  • The MCP4821 DAC pins (CS, SDI and SCK) are connected to the Arduino (10, 11 and 13) digital IO pins.Those Arduino pins serve as SPI bus.
  • A LED is connected to Arduino Analog Pin (A0) and configured as digital IO.
  • This LED, will blink at 500mS duration, whenever the user selects above 4095 millivolt.
  • Thumbwheel switch is a multi-function rotary switch, let user can input codes and numbers easily.
  • Thumbwheel switch is used for generating a binary code that matches the digit.
  • It is one way of easily providing reliable numeric input to the system.
  • Thumbwheel switches are popular in instrumentation, process controls, testing, and machine controls designs.
  • The BCD inputs are multiplexed and connected to the Arduino Digital IO pins 2,3,4 and 5.
  • The digit pins are connected to the Arduino Digital IO pins 6,7,8 and 9.
  • The digits will be selected (Writing to HIGH), and corresponding values are read by the Arduino for further processing.

Step 2: Software

<p>/*<br> Demonstrates the use MCP4821 DAC and Milli Volt generation</p><p> The Arduino circuit connection for Thumbwheel Switch:
 Row connection
 -DATA_0 connected to digital IO 2
 -DATA_1 connected to digital IO 3
 -DATA_2 connected to digital IO 4
 -DATA_3 connected to digital IO 5</p><p> Coloumn selection
 -COL_0 connected to digital IO 6
 -COL_1 connected to digital IO 7
 -COL_2 connected to digital IO 8
 -COL_3 connected to digital IO 9</p><p> MCP4821  Arduino SPI connection
 -SCLK connected to digital IO 13
 -SDI connected to digital IO 11
 -CHIP SELECT connected to digital IO 10
   
 -LED is connected with A0 ADC pin
 
 Name:- M.Pugazhendi
 Date:-  12thJul2016
 Version:- V0.1
 e-mail:- muthuswamy.pugazhendi@gmail.com
 */</p><p>/ /include the library code:
#include <spi.h>
 
const int PIN_CS = 10;
const int GAIN_1 = 0x1;
const int GAIN_2 = 0x0;</spi.h></p><p>const int ROW_0 = 2;
const int ROW_1 = 3;
const int ROW_2 = 4;
const int ROW_3 = 5;</p><p>const int COL_0 = 6;
const int COL_1 = 7;
const int COL_2 = 8;
const int COL_3 = 9;</p><p>const int LED_PIN = A0;</p><p>unsigned int digit1 = 0x00;
unsigned int digit2 = 0x00;
unsigned int digit3 = 0x00;
unsigned int digit4 = 0x00;
unsigned int digit = 0x00;</p><p>void setup()
{
  pinMode(PIN_CS, OUTPUT);</p><p>  pinMode(ROW_0, INPUT);
  pinMode(ROW_1, INPUT);
  pinMode(ROW_2, INPUT);
  pinMode(ROW_3, INPUT);</p><p>  pinMode(COL_0, OUTPUT);
  pinMode(COL_1, OUTPUT);
  pinMode(COL_2, OUTPUT);
  pinMode(COL_3, OUTPUT);</p><p>  pinMode(LED_PIN, OUTPUT);  </p><p>  
  SPI.begin();  
  SPI.setClockDivider(SPI_CLOCK_DIV2);
}
 
//assuming single channel, gain=2
/*
void setOutput(unsigned int val)
{
  byte lowByte = val & 0xff;
  byte highByte = ((val >> 8) & 0xff) | 0x10;
   
  PORTB &= 0xfb;
  SPI.transfer(highByte);
  SPI.transfer(lowByte);
  PORTB |= 0x4;
}
*/
void setOutput(byte channel, byte gain, byte shutdown, unsigned int val)
{
  byte lowByte = val & 0xff;
  byte highByte = ((val >> 8) & 0xff) | channel << 7 | gain << 5 | shutdown << 4;
   
  PORTB &= 0xfb;
  SPI.transfer(highByte);
  SPI.transfer(lowByte);
  PORTB |= 0x4;
}</p><p>void loop()
{</p><p>  digitalWrite(COL_0, HIGH);
  digitalWrite(COL_1, LOW);
  digitalWrite(COL_2, LOW);
  digitalWrite(COL_3, LOW);
  
  digit1 = ((PIND & 0x3C) >> 2);
  
  digitalWrite(COL_0, LOW);
  digitalWrite(COL_1, HIGH);
  digitalWrite(COL_2, LOW);
  digitalWrite(COL_3, LOW);
  
  digit2 = ((PIND & 0x3C) >> 2);</p><p>  digitalWrite(COL_0, LOW);
  digitalWrite(COL_1, LOW);
  digitalWrite(COL_2, HIGH);
  digitalWrite(COL_3, LOW);
  
  digit3 = ((PIND & 0x3C) >> 2);
  
  digitalWrite(COL_0, LOW);
  digitalWrite(COL_1, LOW);
  digitalWrite(COL_2, LOW);
  digitalWrite(COL_3, HIGH);
  
  digit4 = ((PIND & 0x3C) >> 2);
  digit = digit4 + ( digit3 *10) + (digit2 * 100)+ (digit1 * 1000);</p><p>     if( digit <= 0x0FFF)
     {
      //setOutput(digit);
      if( digit <= 0x07FF)
      {
        setOutput(0, GAIN_1, 1, (digit << 1));
      }
      else
      {
        setOutput(0, GAIN_2, 1, digit);
      }
      digitalWrite(LED_PIN, LOW);
     }
     else
     {
       //setOutput(0x0FFF);
       setOutput(0, GAIN_2, 1, 0x0FFF);
       digitalWrite(LED_PIN, !digitalRead(LED_PIN));
     }
     
     //Clear registers     
     digit1 = 0x00;
     digit2 = 0x00;
     digit3 = 0x00;
     digit4 = 0x00;
     digit = 0x00;</p><p>     //500 Milli second delay between reads
     delay(500);
}</p>

Step 3: Conclusion

Picture of Conclusion

  • The Arduino SPI library is used for communicating to the DAC (MCP4821).
  • The thumbwheel switch numerical inputs are read, at every 500mS duration.
  • If the user selected values are less than 2048, then, the DAC gain set to one and internal Vref of DAC is selected as 2.048 Volt.
  • If the user selected values are more than 2048 and less than 4096 then, the DAC gain set to two and internal Vref of DAC is selected as 4.096 Volt.
  • If the user selected values are more than 4096, then, the gain set to one and internal Vref of DAC is selected as 4.096 Volt.
  • A fixed value of 0x0FFFis written to DAC, and the Error LED will blink at 500mS duration.
  • The system can be used to generate the millivolt range between 0mVolt to 4095mVolt.
  • The project is successfully simulated by using the Proteus.
  • The generated miili volt are measured by using the volt meter.

Comments

vivekk134 (author)2017-06-10

it works perfect on UNO but for some reason wont work on Mega ay help available to get it to work on Mega and with Keypad?

vivekk134 made it! (author)2017-05-28

It works sort of still not sure how to get desired effect but work in progress.

vivekk134 (author)2017-05-28

Hi I tried to make this circuit but getting .517 v out put without any input from thumbwheel and maximum output 4.34 Don't know where I went wrong any help ?