Looking for an Arduino board which would help in converting 12 bit binary input to decimal equivalent

Hi. I am new to Arduino and programming. I am trying to convert 12 bit binary input (output of ADC in my case) to decimal value and display it on LED or computer screen. I was searching online and I found that they have done this for 8 bit binary but I want to convert 12 bit binary to decimal. My ADC output can be manipulated as a single 12 bit (16bit) output or two 8 bit outputs that i can interface my ADC output with even an 8 bit microcontroller.
image
Above is the schematic they have used for 8 bit binary to decimal conversion. My question is can i be able to convert 12 bit binary to decimal using the same Arduino UNO board that they have used or please suggest suitable board that can do the job. Also give me some programming ideas. Since I am very new to all this, please explain more detailed way. Thanks a lot.

2 Likes

I don't see the problem. You have commands to print numbers to the OLED, just use those commands to print your 12 bit number. It will automatically print the correct number.
Does your code spend all its time converting all those digital inputs to a number? It is trivial to extend this to 12 bits.
The title to this post is very confusing, because you will never want to convert a 12 bit binary number into decimal. All numbers inside the Arduino ( and all other computers on the planet) use binary numbers. It is only when you want to print them out that they are then converted to decimal, something you don't have to worry about.

Post the code of what you have at the moment.

You may want to read this before you proceed:-
how to get the best out of this forum

It will, amongst other things show you how to post code.

Hi Mike. I want to print the decimal equivalent of the given 12 bit digital output from my
ADC (AD1674). For example, if the output from and ADC is 1111 1111 1111 then I want to convert this to decimal equivalent 4095 and display it to the OLED.
Below is the code they have used to convert the 8 bit binary to decimal using Arduino UNO.

/*

  This sketch converts an 8-Bit Binary number into a Decimal number.
  The Binary number is fed to the Arduino through an 8x DIP Switch.
  A function then converts this Binary number to its Decimal
  equivalent. These numbers are displayed on an OLED Display and Serial   Monitor.

  

*/

//Include the libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//Initialize the variables
int bitVal;
String stringBit;
String stringBinary;
long binaryNumber;
int decimalNumber;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT);

//Prints the EChamp logo on OLED Display
static const uint8_t PROGMEM logo[] = {

  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x07, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x01, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x07, 0xfc, 0x11, 0xf1, 0x0f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  0x0f, 0xfc, 0xf1, 0xf1, 0xe7, 0xfe, 0x00, 0x7e, 0x60, 0x00, 0x30, 0x00, 0x00, 0x30, 0x00, 0x00,
  0x1f, 0xfd, 0xfb, 0xf3, 0xe7, 0xff, 0x00, 0x60, 0x60, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x00, 0x60, 0x63, 0x87, 0xbb, 0xc7, 0x1f, 0x33, 0xcf, 0x00,
  0x3f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x80, 0x60, 0x66, 0xcd, 0xb3, 0x4d, 0x9b, 0x36, 0xdb, 0x00,
  0x3f, 0xfd, 0xf9, 0xf3, 0xe7, 0xff, 0x80, 0x7e, 0x64, 0x48, 0x33, 0x08, 0x99, 0x34, 0x18, 0x00,
  0x7e, 0x40, 0x11, 0xf1, 0x20, 0x1f, 0xc0, 0x60, 0x67, 0xc8, 0x33, 0x08, 0x99, 0x34, 0x1f, 0x00,
  0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xc0, 0x60, 0x64, 0x08, 0x33, 0x08, 0x99, 0x34, 0x01, 0x00,
  0x7e, 0x7d, 0xfb, 0xff, 0xe7, 0xdf, 0xc0, 0x60, 0x66, 0xcd, 0xb3, 0x0d, 0x99, 0x36, 0xdb, 0x00,
  0x7e, 0x7d, 0xff, 0xff, 0xe7, 0xdf, 0xc0, 0x7e, 0x63, 0xc7, 0xbb, 0x07, 0x19, 0x33, 0xde, 0x00,
  0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7c, 0xf1, 0xf1, 0x67, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7e, 0x11, 0xf1, 0x0f, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xfb, 0xf3, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x3c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7f, 0xff, 0xff, 0x7f, 0xdf, 0xe0, 0x7e, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7f, 0x27, 0xf8, 0x9f, 0xdf, 0xc0, 0x62, 0x7c, 0x71, 0xf7, 0x3e, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7e, 0xf1, 0xe1, 0xcf, 0xdf, 0xc0, 0x60, 0x6c, 0x59, 0xbb, 0x36, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7e, 0xf8, 0xe3, 0xef, 0xdf, 0xc0, 0x60, 0x64, 0x09, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x3e, 0x7c, 0xf9, 0xf3, 0xef, 0xdf, 0x80, 0x60, 0x64, 0xf9, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x3f, 0x01, 0xff, 0xff, 0xe0, 0x1f, 0x80, 0x62, 0x64, 0x89, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfc, 0xf9, 0xf3, 0xef, 0xff, 0x80, 0x7e, 0x64, 0x99, 0x91, 0x36, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfe, 0xf8, 0xe3, 0xef, 0xff, 0x00, 0x3c, 0x64, 0xf9, 0x91, 0x3e, 0x00, 0x00, 0x00, 0x00,
  0x0f, 0xfe, 0x71, 0xf1, 0xdf, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  0x0f, 0xff, 0x01, 0xfc, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  0x07, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
  0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x7f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

};

void setup() {

  Serial.begin(9600);

  //Sets pin 3 to pin 10 as input
  for (int x = 3; x < 11; x++) {

    pinMode(x, INPUT);

  }

  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.clearDisplay();

  //Prints the logo on OLED Display
  oled.drawBitmap(0, 0, logo, 128, 64, 1);
  oled.display();
  delay(3000);
  oled.clearDisplay();

}

void loop() {

  //Reads the Binary number from the DIP Switch
  for (int x = 3; x < 11; x++) {

    bitVal = digitalRead(x);
    stringBit = String(bitVal);
    stringBinary = stringBinary + stringBit;
    binaryNumber = stringBinary.toInt();

  }

  //Function to convert Binary to Decimal
  decimalNumber = convertBinaryToDecimal(binaryNumber);

  //Prints the Binary number on the Serial Monitor
  Serial.print("Binary: ");
  Serial.print(stringBinary);
  Serial.print("      ");

  //Prints the Decimal number on the Serial Monitor
  Serial.print("Decimal: ");
  Serial.println(decimalNumber);

  //Prints the Binary number on the Serial Monitor
  oled.clearDisplay();
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(5, 10);
  oled.print("B:");
  oled.println(stringBinary);

  //Prints the Decimal number on the Serial Monitor
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(5, 40);
  oled.print("D:");
  oled.println(decimalNumber);
  oled.display();

  //Resets all the variables
  binaryNumber = 0;
  bitVal = 0;
  stringBit = "";
  stringBinary = "";

}

//Function to convert Binary to Decimal
long convertBinaryToDecimal(long binary) {

  long number = binary;
  long decimalVal = 0;
  long baseVal = 1;
  long tempVal = number;
  long previousDigit;

  while (tempVal) {

    //Converts Binary to Decimal
    previousDigit = tempVal % 10;
    tempVal = tempVal / 10;
    decimalVal += previousDigit * baseVal;
    baseVal = baseVal * 2;

  }

  //Returns the Decimal number
  return decimalVal;

}
1 Like

Which ADC?

Hi Sterretje. I am using AD1674 from Analog Devices.
image

No you don't.
Did you write that code? Who ever wrote that binary to decimal function had no idea what they were doing, or were trying to do something different to what you want to do.

For reading a 12 bit A/D, assuming it is the built in one you simply do.

value = analogRead(number);
oled.println(value);

/*

  This sketch converts an 8-Bit Binary number into a Decimal number.
  The Binary number is fed to the Arduino through an 8x DIP Switch.
  A function then converts this Binary number to its Decimal
  equivalent. These numbers are displayed on an OLED Display and Serial   Monitor.

  

*/

//Include the libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//Initialize the variables
int bitVal;
long binaryNumber;

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT);

//Prints the EChamp logo on OLED Display
static const uint8_t PROGMEM logo[] = {

  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x07, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x01, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x07, 0xfc, 0x11, 0xf1, 0x0f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  0x0f, 0xfc, 0xf1, 0xf1, 0xe7, 0xfe, 0x00, 0x7e, 0x60, 0x00, 0x30, 0x00, 0x00, 0x30, 0x00, 0x00,
  0x1f, 0xfd, 0xfb, 0xf3, 0xe7, 0xff, 0x00, 0x60, 0x60, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x00, 0x60, 0x63, 0x87, 0xbb, 0xc7, 0x1f, 0x33, 0xcf, 0x00,
  0x3f, 0xfd, 0xff, 0xff, 0xe7, 0xff, 0x80, 0x60, 0x66, 0xcd, 0xb3, 0x4d, 0x9b, 0x36, 0xdb, 0x00,
  0x3f, 0xfd, 0xf9, 0xf3, 0xe7, 0xff, 0x80, 0x7e, 0x64, 0x48, 0x33, 0x08, 0x99, 0x34, 0x18, 0x00,
  0x7e, 0x40, 0x11, 0xf1, 0x20, 0x1f, 0xc0, 0x60, 0x67, 0xc8, 0x33, 0x08, 0x99, 0x34, 0x1f, 0x00,
  0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xc0, 0x60, 0x64, 0x08, 0x33, 0x08, 0x99, 0x34, 0x01, 0x00,
  0x7e, 0x7d, 0xfb, 0xff, 0xe7, 0xdf, 0xc0, 0x60, 0x66, 0xcd, 0xb3, 0x0d, 0x99, 0x36, 0xdb, 0x00,
  0x7e, 0x7d, 0xff, 0xff, 0xe7, 0xdf, 0xc0, 0x7e, 0x63, 0xc7, 0xbb, 0x07, 0x19, 0x33, 0xde, 0x00,
  0x7e, 0x7d, 0xf9, 0xf3, 0xe7, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7c, 0xf1, 0xf1, 0x67, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7e, 0x11, 0xf1, 0x0f, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xfb, 0xf3, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xfe, 0x7f, 0xff, 0xff, 0xff, 0xdf, 0xe0, 0x3c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7f, 0xff, 0xff, 0x7f, 0xdf, 0xe0, 0x7e, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7f, 0x27, 0xf8, 0x9f, 0xdf, 0xc0, 0x62, 0x7c, 0x71, 0xf7, 0x3e, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7e, 0xf1, 0xe1, 0xcf, 0xdf, 0xc0, 0x60, 0x6c, 0x59, 0xbb, 0x36, 0x00, 0x00, 0x00, 0x00,
  0x7e, 0x7e, 0xf8, 0xe3, 0xef, 0xdf, 0xc0, 0x60, 0x64, 0x09, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x3e, 0x7c, 0xf9, 0xf3, 0xef, 0xdf, 0x80, 0x60, 0x64, 0xf9, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x3f, 0x01, 0xff, 0xff, 0xe0, 0x1f, 0x80, 0x62, 0x64, 0x89, 0x91, 0x32, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfc, 0xf9, 0xf3, 0xef, 0xff, 0x80, 0x7e, 0x64, 0x99, 0x91, 0x36, 0x00, 0x00, 0x00, 0x00,
  0x1f, 0xfe, 0xf8, 0xe3, 0xef, 0xff, 0x00, 0x3c, 0x64, 0xf9, 0x91, 0x3e, 0x00, 0x00, 0x00, 0x00,
  0x0f, 0xfe, 0x71, 0xf1, 0xdf, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  0x0f, 0xff, 0x01, 0xfc, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
  0x07, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
  0x03, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x01, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x1f, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x07, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x7f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00

};

const byte adcPin[12] = {3,4,5,6,7,8,9,10,11,12,13,14};

void setup() {

  Serial.begin(9600);

  //Sets pin 3 to pin 10 as input
  for (int x = 0; x < 12; x++) {

    pinMode(adcPin[x], INPUT);

  }

  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.clearDisplay();

  //Prints the logo on OLED Display
  oled.drawBitmap(0, 0, logo, 128, 64, 1);
  oled.display();
  delay(3000);
  oled.clearDisplay();

}

void loop() {

  //Reads the Binary number from the DIP Switch
  for (int x = 0; x < 12; x++) {

    bitVal = digitalRead(adcPin[x]);
    if (bitVal == HIGH)
      bitSet(binaryNumber, x);
    else
      bitClear(binaryNumber, x);
  }

  //Prints the Binary number on the Serial Monitor
  Serial.print("Binary: ");
  Serial.print(binaryNumber, BIN);
  Serial.print("      ");

  //Prints the Decimal number on the Serial Monitor
  Serial.print("Decimal: ");
  Serial.println(binaryNumber);

  //Prints the Binary number on the Serial Monitor
  oled.clearDisplay();
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(5, 10);
  oled.print("B:");
  oled.println(binaryNumber, BIN);

  //Prints the Decimal number on the Serial Monitor
  oled.setTextSize(2);
  oled.setTextColor(WHITE);
  oled.setCursor(5, 40);
  oled.print("D:");
  oled.println(binaryNumber);
  oled.display();

}

Missed your actual example, it was posted while I wrote my first reply, but this is easy as well,
Assuming you have read the two bytes above let's call them byte1 and byte2

value = (byte2 >> 4) | (byte1 << 4);
oled.println(value);

The >> is a shift right operator
The << is a shift left operator
The | is a bitwise OR operator for combining the two values.

Doing this to handle the dip switches

And the associated function to convert a long made of 1 and 0 into a binary number was really a bad idea written by someone who just does not have enough experience.

Don’t do that. Throw away that code, never use it again…

As said by others you get a number already from your ADC just use it as it is.

Sorry Mike, I couldn't quite get what you are saying. Are you saying that I just have to give the digital 12 bit input to Arduino digital I/O pins and using those code that you have wrote to display it as Decimal equivalent in OLED. Please explain the schematic. Please apologize, i am very new to all this.

What schematic? I only posted code.

No. You get the input from your A/D not the I/O pins.

Yes

I am assuming that you did not write that code you are using and have no idea what it does. Throw it away and start again because the person who did write it had no idea what they were doing. They were converting a number into a what is called a BCD (Binary Coded Decimal) string and then printing out the string. They didn't know that the display.print(number) call does all that for you.

Regarding what I did in post 8, I took the two bytes you read from your A/D and shifted them so that byte2 contained only the four least significant bits from your A/D and bits 11 to 4 of byte1 contained only the 8 most significant bits of the D/A converter. Then I merged those two bytes into one using the OR operation.
To fully understand these operators please read:-
Bitwise OR

Bitwise shift left

Bitwise shift right

1 Like

Thanks for explaining post#8. I understood that. I do need some sort of hardware arrangement to read that ADCs 12 bit binary output and display it on OLED using your code. Can i use Arduino UNO to do this job? Thanks again for your help.

Yes you need to connect your external A/D, the AD1674 to your Arduino. Then you need some software to trigger the conversion and when it is finished read the two result bytes.

Have you got a AD1674 chip or have you got a board that contains a AD1674. Please provide a link to what you have.

the 12 bits are on 12 pins of your module


you need either to connect those to 12 pins of your Arduino and sample them or use some Parallel-in/ serial-out shift registers to read all 12 bits (for example) if you don't have 12 pins available.

if you read those bits directly into the right bit position of a uint16_t (2 byte unsigned integral number) then you've got your value.

(of course as @Grumpy_Mike explained, you also need to read the spec to trigger a conversion and configure the module)

1 Like

Actually AD1674 sits on a board which is controlled by an onbaord FPGA. I do not have to worry about that. My only job is to read the digital 12 bit data whenever it comes and display it as a decimal equivalent on OLED. FYI the input to the ADC is an analog voltage in the range of 1 to 10 V.

You will need access to some other pins as well, to see if the conversion has finished.

Also you will need to know what mode this A/D is being operated in, that is 8bit or 12 bit and does it change?

Depending on the answers to this question will determine how you need to wire it up, and write your software.

It would have been good to know this at the ousted because the code examples I have given you made assumptions that now seem to be not valid.

yes Mike, there is an STS pin in an ADC which can tell me if the conversion is completed or in progress (Its an active high).

It will operate in 12 bit resolution only but it can either give one single 12 bit output or two 8 bits (8 bits in one go and 8 bit (which has 4 LSB and 4 trailing zeros) in second as per my setting of A0 pin (short cycle pin on ADC) (controlled by fpga).

If I set the ADC in 12 bit output mode (It will organize output as 12 bit output in one go) and give those 12 bits to Arduino 12 digital I/O pins (Ardunio UNO has 14 I/O pins it seems) and then do the coding to display this as an equivalent decimal number on OLED. Now can you please suggest me the suitable coding ? Thanks

I don't think it can operate in any other mode.

I thought you said that you have no control over the board?
You said:-

Now you are saying that you can control the output mode. Which is it?

If it is controlled by the FPGA then you by definition can not control it.

It is only with accurate consistent information can we help you.

Only when I know what is going on and what is controlled by the FPGA and what is controlled by you.

Hi Mike. Sorry for confusing you with my language. Below is the sketch of the board that I have.

  • input to the ADC is from external analog voltage signals of 1-10 V range
  • ADC reference voltage is 10V. This ADC conversion cycle is controlled by FPGA (I do not write the code, but I can tell the FPGA guy to write a code such that the output can be organized as single 12 bit or two 8 bits. you are right, it will operate in 12 bit resolution only.
  • D0 to D11 binary bits output from ADC is what I am working on. I just have to design some hardware to read that 12 bit data and display it on OLED in decimal equivalent of those 12 bit binary data. That is when I came across Arduino. I do not have any prior experience in that as well.
  • what i thought was, I will use some Arduino board to do this operation. And learn some coding as well.
    image

This is the whole scenario.

Hi Jack. Can you please explain this in detail. Thanks