Categories

Follow Us

Arduino and MCP3421 A/D converter example

The MCP3421 ADC can be used for various high accuracy analog-to-digital data conversion applications where ease of use, low power consumption and small footprint are major considerations.The MCP3421 is a single channel low-noise, high accuracy delta-sigma A/D converter with differential inputs and up to 18 bits of resolution in a small SOT-23-6 package.

The on-board precision 2.048V reference voltage enables a differential input range of ±2.048V. The device uses a two-wire I2C™ compatible interface and operates from a single power supply ranging from 2.7V to 5.5V. The MCP3421 ADC performs conversions at rates of 3.75, 15, 60 or 240 samples per second with corresponding resolutions of 18, 16, 14 and 12 bits. The onboard programmable gain amplifier (PGA) provides gain up to 8x. The device has two conversion modes: Continuous mode and One-Shot mode. In One-Shot mode, the device enters a low current standby mode automatically after a conversion, greatly reducing power use.

Features
    • 18-bit resolution
    • Small 6-lead SOT-23 packaging
    • Differential input operation
    • On-board voltage reference with 5 ppm/°C drift
    • On-board PGA, gains of 1, 2, 4, 8
    • Programmable data rate options
      • 3.75 SPS (18 bits)
      • 15 SPS (16 bits)
      • 60 SPS (14 bits)
      • 240 SPS (12 bits)
    • INL 10 ppm of FSR max
    • Low current consumption, 145 µA at 3V
    • One-shot or continuous conversion options
    • Supports I2C™ serial interface
    • Extended temperature range: -40°C to +125°C

Parts List

Part Link
MCP3421 MCP3421 I2C SOT23-6 delta-sigma ADC Evaluation Board
Connecting cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire Dupont cablefor Arduino
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO R3

 

Connection

Its an I2C device – I connected the VIN- to Gnd and Vin+ to Pin 6

 

Code

You need to download and install the following library – https://github.com/uChip/MCP342X

// Include libraries this sketch will use
#include  <Wire.h>
#include  <MCP342X.h>
 
// Instantiate objects used in this project
MCP342X myADC;
int outputPin = 6;
 
void setup() 
{
  Wire.begin();  // join I2C bus
  TWBR = 12;  // 400 kHz (maximum)
 
  Serial.begin(9600); // Open serial connection to send info to the host
  while (!Serial) {}  // wait for Serial comms to become ready
  Serial.println("Starting up");
  Serial.println("Testing device connection...");
  Serial.println(myADC.testConnection() ? "MCP342X connection successful" : "MCP342X connection failed");
 
  myADC.configure( MCP342X_MODE_CONTINUOUS |
                   MCP342X_CHANNEL_1 |
                   MCP342X_SIZE_16BIT |
                   MCP342X_GAIN_1X
                 );
 
  Serial.println(myADC.getConfigRegShdw(), HEX);
 
}  // End of setup()
 
void loop() {
  static int16_t  result;
  for(int i=0; i<=255; i++)
  {
    myADC.startConversion();
    analogWrite(outputPin, i);
    myADC.getResult(&result);
    Serial.print(i);
    Serial.print("  ");
    Serial.print(result);
    Serial.print("  ");
    Serial.println(result, HEX);
  }
 
}  // End of loop()

 

link

Datasheet – http://ww1.microchip.com/downloads/en/DeviceDoc/22003e.pdf

Share

Arduino and MMA8452 sensor example

In this example we will connect a MMA8452Q accelerometer  to an Arduino Uno

Lets look at some information about the sensor

The MMA8452Q is a smart, low-power, three-axis, capacitive, micromachined accelerometer with 12 bits of resolution. This accelerometer is packed with embedded functions with flexible user programmable options, configurable to two interrupt pins. Embedded interrupt functions allow for overall power savings relieving the host processor from continuously polling data.

The MMA8452Q has user selectable full scales of ±2 g/±4 g/±8 g with high-pass filtered data as well as non-filtered data available real-time. The device can be configured to generate inertial wakeup interrupt signals from any combination of the configurable embedded functions allowing the MMA8452Q to monitor events and remain in a low-power mode during periods of inactivity

 

Connection

Here is a layout, its an easy device to connect

Arduino and MMA8452

Arduino and MMA8452

Parts List

Here are the parts I used

Part name Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO R3
MMA8452 saensor GY-45 MMA8452 Module Digital Triaxial Accelerometer Precision Tilt 3-Axis
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire Dupont cablefor Arduino

 

Code

There is a library from sparkfun but there is this example which does not require any libraries

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MMA8452Q
// This code is designed to work with the MMA8452Q_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Accelorometer?sku=MMA8452Q_I2CS#tabs-0-product_tabset-2
 
#include <Wire.h>
 
// MMA8452Q I2C address is 0x1C(28)
#define Addr 0x1C
 
void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise Serial Communication, set baud rate = 9600
  Serial.begin(9600);
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register
  Wire.write(0x2A);
  // StandBy mode
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register
  Wire.write(0x2A);
  // Active mode
  Wire.write(0x01);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control register
  Wire.write(0x0E);
  // Set range to +/- 2g
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(300);
}
 
void loop()
{
  unsigned int data[7];
 
  // Request 7 bytes of data
  Wire.requestFrom(Addr, 7);
 
  // Read 7 bytes of data
  // staus, xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
  if(Wire.available() == 7) 
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
    data[6] = Wire.read();
  }
 
  // Convert the data to 12-bits
  int xAccl = ((data[1] * 256) + data[2]) / 16;
  if (xAccl > 2047)
  {
    xAccl -= 4096;
  }
 
  int yAccl = ((data[3] * 256) + data[4]) / 16;
  if (yAccl > 2047)
  {
    yAccl -= 4096;
  }
 
  int zAccl = ((data[5] * 256) + data[6]) / 16;
  if (zAccl > 2047)
  {
    zAccl -= 4096;
  }
 
  // Output data to serial monitor
  Serial.print("Acceleration in X-Axis : ");
  Serial.println(xAccl);
  Serial.print("Acceleration in Y-Axis : ");
  Serial.println(yAccl);
  Serial.print("Acceleration in Z-Axis : ");
  Serial.println(zAccl);
  delay(500);
}

 

Output

Open the serial monitor – this is what you should expect to see

Acceleration in X-Axis : -186
Acceleration in Y-Axis : 330
Acceleration in Z-Axis : 125
Acceleration in X-Axis : -784
Acceleration in Y-Axis : 426
Acceleration in Z-Axis : 76
Acceleration in X-Axis : -225
Acceleration in Y-Axis : 890
Acceleration in Z-Axis : -273
Acceleration in X-Axis : -199
Acceleration in Y-Axis : 1053
Acceleration in Z-Axis : -260

 

Links

https://www.nxp.com/docs/en/data-sheet/MMA8452Q.pdf

Share

ADS1115 analog-to-digital converter and Arduino

The ADS1115 device is a precision, low-power, 16-bit, I2C-compatible, analog-to-digital converters (ADCs) offered in an ultra-small, leadless, X2QFN-10 package, and a VSSOP-10 package. The  device incorporates a low-drift voltage reference and an oscillator. The converter also incorporates a programmable gain amplifier and a digital comparator. These features, along with a wide operating supply range, make the converter well suited for power- and space-constrained, sensor measurement applications.

The ADS1115 perform conversions at data rates up to 860 samples per second (SPS). The PGA offers input ranges from ±256 mV to ±6.144 V, allowing precise large- and small-signal measurements. The converter features an input multiplexer  that allows two differential or four single-ended input measurements. Use the digital comparator in the ADS1115 for under- and overvoltage detection.

The ADS1115 operates in either continuous-conversion mode or single-shot mode. The devices are automatically powered down after one conversion in single-shot mode; therefore, power consumption is significantly reduced during idle periods.

ADS1115 Features

Wide Supply Range: 2.0 V to 5.5 V
Low Current Consumption: 150 µA
(Continuous-Conversion Mode)
Programmable Data Rate: 8 SPS to 860 SPS
Single-Cycle Settling
Internal Low-Drift Voltage Reference
Internal Oscillator
I2C Interface: Four Pin-Selectable Addresses
Four Single-Ended or Two Differential Inputs
Programmable Comparator
Operating Temperature Range: –40°C to +125°C

 

Parts List

This module will cost less than $2

Amount Part Type
1 ADS1115
1 UNO R3 CH340G/ATmega328P, compatible for Arduino UNO R3

 

Schematics/Layout

 

In the layout below we just show the Arduino and sensor , for test purposes we connect A0 to A3 to various voltages for test purposes

 

arduino and ads1115 layout

arduino and ads1115 layout

 

 

arduino and ads1115 schematic

arduino and ads1115 schematic

 

 

Code

Again we use a library and again its an adafruit one – https://github.com/adafruit/Adafruit_ADS1X15

 

#include <Wire.h>
#include <Adafruit_ADS1015.h>
 
Adafruit_ADS1115 ads(0x48);
 
void setup(void)
{
Serial.begin(9600);
Serial.println("Hello!");
 
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
 
ads.begin();
}
 
void loop(void)
{
int16_t adc0, adc1, adc2, adc3;
 
adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1);
adc2 = ads.readADC_SingleEnded(2);
adc3 = ads.readADC_SingleEnded(3);
Serial.print("AIN0: ");
Serial.println(adc0);
Serial.print("AIN1: ");
Serial.println(adc1);
Serial.print("AIN2: ");
Serial.println(adc2);
Serial.print("AIN3: ");
Serial.println(adc3);
Serial.println(" ");
 
delay(1000);
}

 

Output

Open the serial monitor and you should see something like this

AIN0: 17707
AIN1: -1
AIN2: 26295
AIN3: -1

AIN0: 17702
AIN1: -1
AIN2: 26291
AIN3: -1

AIN0: 17706
AIN1: -1
AIN2: 26289
AIN3: -1

AIN0: 17700
AIN1: -1
AIN2: 26349
AIN3: -1

 

Video

In this video we compile and program the Arduino and show you the output. We also show a layout and schematic in fritzing, we tied the A0 to A3 to various voltages, at the end we removed these so the input is ‘floating’

 

Links

http://www.ti.com/lit/ds/symlink/ads1115.pdf

I2C ADS1115 16 Bit ADC 4 channel Module with Programmable Gain Amplifier 2.0V to 5.5V RPi

Share

CCS811 digital gas sensor and Arduino example

In this example we will connect a CCS811 gas sensor to an arduino, lets look at the sensor

CCS811 is a low-power digital gas sensor solution, which integrates a gas sensor solution for detecting low levels of VOCs typically found indoors, with a microcontroller unit (MCU) and an Analog-to-Digital converter to monitor the local environment and provide an indication of the indoor air quality via an equivalent CO2 or TVOC output over a standard I2C digital interface.

Features

Integrated MCU
On-board processing
Standard digital interface
Optimised low power modes
IAQ threshold alarms
Programmable baseline
2.7mm x 4.0mm LGA package
Low component count
Proven technology platform

Specs

Interface I²C
Supply Voltage [V] 1.8 to 3.6
Power Consumption [mW] 1.2 to 46
Dimension [mm] 2.7 x 4.0 x 1.1 LGA
Ambient Temperature Range [°C] -40 to 85
Ambient Humidity Range [% r.h.] 10 to 95

Parts List

 

 

Amount Part Type
1 CJMCU-811 CCS811 Air Quality Gas Sensor
1 UNO R3 CH340G/ATmega328P, compatible for Arduino UNO R3

 

 

Schematics/Layout

 

Remember and connect WAKE to gnd

arduino and ccs811 layout

arduino and ccs811 layout

 

arduino and ccs811 schematic

arduino and ccs811 schematic

Code

Again we use a library this is the adafruit one – you can use the library manager and add this.

And this is the out of the box example

#include "Adafruit_CCS811.h"
Adafruit_CCS811 ccs;
void setup() {
Serial.begin(9600);
Serial.println("CCS811 test");
if(!ccs.begin()){
Serial.println("Failed to start sensor! Please check your wiring.");
while(1);
}
//calibrate temperature sensor
while(!ccs.available());
float temp = ccs.calculateTemperature();
ccs.setTempOffset(temp - 25.0);
}
void loop() {
if(ccs.available()){
float temp = ccs.calculateTemperature();
if(!ccs.readData()){
Serial.print("CO2: ");
Serial.print(ccs.geteCO2());
Serial.print("ppm, TVOC: ");
Serial.print(ccs.getTVOC());
Serial.print("ppb Temp:");
Serial.println(temp);
}
else{
Serial.println("ERROR!");
while(1);
}
}
delay(500);
}

 

Output

Open the serial monitor – this is what I saw. The higher CO2 level was when I breathed on the sensor

CO2: 400ppm, TVOC: 0ppb Temp:28.28
CO2: 400ppm, TVOC: 0ppb Temp:47.75
CO2: 400ppm, TVOC: 0ppb Temp:25.00
CO2: 400ppm, TVOC: 0ppb Temp:26.49
CO2: 1228ppm, TVOC: 126ppb Temp:25.68
CO2: 575ppm, TVOC: 26ppb Temp:9.59
CO2: 400ppm, TVOC: 0ppb Temp:26.49
CO2: 400ppm, TVOC: 0ppb Temp:105.81

 

Video

In this video we import the library, we compile and program the Arduino and then we open the serial monitor. The higher reading is when I blew on the sensor

Links

ccs811 datasheet

CJMCU-811 CCS811 Air Quality Gas Sensor

Share