ADXL337 accelerometer and Arduino Due example

In this article we connect an ADXL337 accelerometer to an Arduino Due

The ADXL337 is a small, thin, low power, complete 3-axis accelerometer with signal conditioned voltage outputs. The product measures acceleration with a minimum full-scale range of ±3g. It can measure the static acceleration of gravity in tiltsensing applications, as well as dynamic acceleration resulting from motion, shock, or vibration.

The user selects the bandwidth of the accelerometer using the CX, CY, and CZ capacitors at the XOUT, YOUT, and ZOUTpins. Bandwidths can be selected to suit the application, with a range of 0.5 Hz to 1600 Hz for X and Y axes, and a range of 0.5 Hz to 550 Hz for the Z axis.

 

Parts List

Name Link
Arduino Due Black Due R3 Board DUE-CH340 ATSAM3X8E ARM Main Control Board with 50cm USB Cable CH340G for arduino
ADXL337 3-axis ADXL337 GY-61 Replacement ADXL335 Module Analog Output Accelerometer
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic

 

arduino due and adxl337

arduino due and adxl337

 

Code

No libraries required – this is a sparkfun example

// Make sure these two variables are correct for your setup
int scale = 3; // 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
 
 
void setup()
{
  // Initialize serial communication at 115200 baud
  Serial.begin(115200);
}
 
// Read, scale, and print accelerometer data
void loop()
{
  // Get raw accelerometer data for each axis
  int rawX = analogRead(A0);
  int rawY = analogRead(A1);
  int rawZ = analogRead(A2);
 
  // Scale accelerometer ADC readings into common units
  // Scale map depends on if using a 5V or 3.3V microcontroller
  float scaledX, scaledY, scaledZ; // Scaled values for each axis
  scaledX = mapf(rawX, 0, 675, -scale, scale); // 3.3/5 * 1023 =~ 675
  scaledY = mapf(rawY, 0, 675, -scale, scale);
  scaledZ = mapf(rawZ, 0, 675, -scale, scale);
 
  // Print out raw X,Y,Z accelerometer readings
  Serial.print("X: "); Serial.println(rawX);
  Serial.print("Y: "); Serial.println(rawY);
  Serial.print("Z: "); Serial.println(rawZ);
  Serial.println();
 
  // Print out scaled X,Y,Z accelerometer readings
  Serial.print("X: "); Serial.print(scaledX); Serial.println(" g");
  Serial.print("Y: "); Serial.print(scaledY); Serial.println(" g");
  Serial.print("Z: "); Serial.print(scaledZ); Serial.println(" g");
  Serial.println();
 
  delay(2000); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
}
 
// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

 

Output

Open the serial monitor and you will see something like this

X: 612
Y: 500
Z: 554

X: 2.44 g
Y: 1.44 g
Z: 1.92 g

X: 619
Y: 478
Z: 510

X: 2.50 g
Y: 1.25 g
Z: 1.53 g

 

Links

https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL337.pdf

Share

Arduino Due and VEML6040 color sensor example

VEML6040 color sensor senses red, green, blue, and white light and incorporates photodiodes, amplifiers, and analog / digital circuits into a single chip using CMOS process.

With the color sensor applied, the brightness, and color temperature of backlight can be adjusted base on ambient light source that makes panel looks more comfortable for end user’s eyes. VEML6040’s adoption of FiltronTM technology achieves the closest ambient light spectral sensitivity to real human eye responses.

VEML6040 provides excellent temperature compensation capability for keeping the output stable under changing temperature. VEML6040’s function are easily operated via the simple command format of I2C (SMBus compatible) interface protocol. VEML6040’s operating voltage ranges from 2.5 V to 3.6 V.

Parts List

Name Link
Arduino Due Black Due R3 Board DUE-CH340 ATSAM3X8E ARM Main Control Board with 50cm USB Cable CH340G for arduino
VEML6040 WAVGAT 3.3V I2C Digital RGBW Color Sensor VEML6040 Breakout For Arduino
Connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Connection

Arduino Due VEML6040 module
3v3 Vin
Gnd Gnd
SCA – 20 SCA
SCL – 21 SCL

 

Code

Again we use a library – https://github.com/thewknd/VEML6040

This example worked just fine

WP-SYNHIGHLIGHT PLUGIN: NOTHING TO HIGHLIGHT! PLEASE READ README.TXT IN PLUGIN FOLDER!

#include "Wire.h"
#include "veml6040.h"

VEML6040 RGBWSensor;

void setup() {
Serial.begin(9600);
Wire.begin();
if(!RGBWSensor.begin()) {
Serial.println("ERROR: couldn't detect the sensor");
while(1){}
}

/*
* init RGBW sensor with:
* - 320ms integration time
* - auto mode
* - color sensor enable
*/

RGBWSensor.setConfiguration(VEML6040_IT_320MS + VEML6040_AF_AUTO + VEML6040_SD_ENABLE);

delay(1500);
Serial.println("Vishay VEML6040 RGBW color sensor auto mode example");
Serial.println("CCT: Correlated color temperature in \260K");
Serial.println("AL: Ambient light in lux");
delay(1500);
}

void loop() {
Serial.print("RED: ");
Serial.print(RGBWSensor.getRed());
Serial.print(" GREEN: ");
Serial.print(RGBWSensor.getGreen());
Serial.print(" BLUE: ");
Serial.print(RGBWSensor.getBlue());
Serial.print(" WHITE: ");
Serial.print(RGBWSensor.getWhite());
Serial.print(" CCT: ");
Serial.print(RGBWSensor.getCCT());
Serial.print(" AL: ");
Serial.println(RGBWSensor.getAmbientLight());
delay(400);
}

[/cpp]

 

Output

Open the serial monitor - this is what I saw

Vishay VEML6040 RGBW color sensor auto mode example
CCT: Correlated color temperature in ⸮K
AL: Ambient light in lux
RED: 947 GREEN: 947 BLUE: 947 WHITE: 947 CCT: 10144 AL: 29.79
RED: 949 GREEN: 949 BLUE: 949 WHITE: 949 CCT: 10144 AL: 29.86
RED: 947 GREEN: 947 BLUE: 947 WHITE: 947 CCT: 10144 AL: 29.79
RED: 949 GREEN: 949 BLUE: 949 WHITE: 949 CCT: 10144 AL: 29.86
RED: 949 GREEN: 949 BLUE: 949 WHITE: 949 CCT: 10144 AL: 29.86
RED: 948 GREEN: 948 BLUE: 948 WHITE: 948 CCT: 10144 AL: 29.82

Links

https://www.vishay.com/docs/84276/veml6040.pdf

VEML6040 Breakout Vishay RGBW Sensor Module

Share

Arduino Due and MCP3424 A/D converter example

Arduino Due and MCP3424 A/D converter example, lets look at the sensor.

The MCP3424 is a four channel low-noise, high accuracy delta-sigma A/D converter with differential inputs and up to 18 bits of resolution. The on-board precision 2.048V reference voltage enables an input range of ±2.048V differentially. The device uses a two-wire I2C™ compatible serial interface and operates from a single power supply ranging from 2.7V to 5.5V.

The MCP3424 device performs conversions at rates of 3.75, 15, 60 or 240 samples per second depending on user controllable configuration bit settings using the two-wire I2C™ compatible serial interface. The I2C™ address is user configurable with two address selection pins. This device has an onboard programmable gain amplifier (PGA). User can select the PGA gain of x1, x2, x4, or x8 before the analog-to-digital conversion takes place. This allows the MCP3424 device to convert a smaller input signal with high resolution. The device has two conversion modes: (a) Continuous mode and (b) One-Shot mode.

In One-Shot mode, the device enters a low current standby mode automatically after one conversion. This reduces current consumption greatly during idle periods. The MCP3424 device 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.

Features
    • 18-bit resolution
    • 4-channel differential input operation
    • Differential input operation
    • On-board voltage reference with 15 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, 135 µA at 3V
    • One-shot or continuous conversion options
    • Supports I2C™ serial interface with user configurable addresses
    • Extended temperature range: -40°C to +125°C

 

Parts List

Part Link
Arduino Due Black Due R3 Board DUE-CH340 ATSAM3X8E ARM Main Control Board with 50cm USB Cable CH340G for arduino
MCP3424 module MCP3424 Digital I2C ADC-4 Channel Conversion Module
Connecting cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Code

You need to install the following library – https://github.com/bersch/MCP3424

#include <Wire.h>
#include <MCP3424.h>
 
MCP3424 adc(PIN_FLOAT, PIN_FLOAT);
 
void setup() 
{
    Serial.begin(9600);
    Wire.begin();
    adc.generalCall(GC_RESET);
    adc.creg[CH1].bits = { GAINx1, SR18B, CONTINUOUS, CH1, 1 };
}
 
double value;
static char * errmsg[] = {"", "underflow", "overflow", "i2c", "in progress", "timeout"};
 
void loop() 
{
    ConvStatus err = adc.read(CH1, value);
    if (err == R_OK) 
      Serial.println(value, DEC); 
    else 
    {
      Serial.print("conversion error: ");
      Serial.println(errmsg[err]);
    }
    asm volatile ("nop");
}

Output

Open the serial monitor – the low reading was ground and the higher reading was 3.3v

 

Share

Arduino Due 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 Due Black Due R3 Board DUE-CH340 ATSAM3X8E ARM Main Control Board with 50cm USB Cable CH340G for arduino

 

Connection

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

Arduino Due VEML6040 module
3v3 Vcc
Gnd Gnd
SDA – 20 SDA
SCL – 21 SCL

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 Due : AM2320 temperature and humidity sensor

Temperature and humidity combined sensor AM2320 digital temperature and humidity sensor is a digital signal output has been calibrated. Using special temperature and humidity acquisition technology, ensure that the product has a very high reliability and excellent long-term stability. Sensor consists of a capacitive moisture element and an integrated high-precision temperature measurement devices, and connected with a high-performance microprocessor .

AM2320 communication using a single bus, two communication modes standard I2C. Standard single-bus interface, the system integration becomes easy and quick. Ultra-small size, low power consumption, signal transmission distance up to 20 meters, making all kinds of applications and even the most demanding applications the best choice. I2C communication using standard communication sequence, the user can directly linked to the I2C communication bus without additional wiring, simple to use. Two communication modes are used as humidity, temperature, and other digital information directly CRC checksum temperature-compensated output, users do not need to calculate the secondary digital output, and no need for temperature compensation of the humidity, temperature and humidity can be accurately information. Two communication modes are free to switch, the user can freely choose, easy to use, wide range of applications.

 

Specifications

• Operating Voltage: 3.1 VDC to 5.5 VDC
• Operating Temperature Range: -40 ° C to + 80 ° C
• Humidity Range: 0 to 99.9% RH
• Accuracy ( 25 ° C environment)
Temperature: ± 0.5 ° C
Humidity: ± 3%
• RH (10 … 90% RH)
Resolution: Temperature: 0.1 ° C
Resolution: Humidity: 0.1% RH
• Attenuation values
Temperature: <0.1 ℃ / Year
Humidity: <1% RH / Year
• Response time: Temperature: 5s
• Response Time: Humidity: 5s 1 / e (63%)
• Output signal: single bus / IIC signal
• Housing material: PC plastic

Parts List

name Link
Arduino Due Black Due R3 Board DUE-CH340 ATSAM3X8E ARM Main Control Board with 50cm USB Cable CH340G for arduino
AM2320 AM2320 Digital Temperature&Humidity Sensor Module Single Bus I2C
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Connection

Arduino Due AM2320 module
3v3 +
Gnd
SDA – 20 SDA
SCL – 21 SCL

 

Code

You will need to install the folllowing library from https://github.com/EngDial/AM2320

This is the default example

#include <AM2320.h>
 
AM2320 th(&Wire);
 
void setup() {
  Serial.begin(9600);
  Wire.begin();
}
 
void loop() {
  Serial.println(F("Chip = AM2320"));
  switch(th.Read()) {
    case 2:
      Serial.println(F("  CRC failed"));
      break;
    case 1:
      Serial.println(F("  Sensor offline"));
      break;
    case 0:
      Serial.print(F("  Humidity = "));
      Serial.print(th.Humidity);
      Serial.println(F("%"));
      Serial.print(F("  Temperature = "));
      Serial.print(th.cTemp);
      Serial.println(F("*C"));
      Serial.println();
      break;
  }
  delay(2000);
}

Output

Open the serial monitor

Chip = AM2320
Humidity = 29.90%
Temperature = 29.60*C

Chip = AM2320
Humidity = 30.10%
Temperature = 29.40*C

Chip = AM2320
Humidity = 30.30%
Temperature = 29.30*C

 

Links

AM2320 Digital Temperature and Humidity Sensor Replace AM2302 SHT10

https://akizukidenshi.com/download/ds/aosong/AM2320.pdf

Share