Categories

Arduino and ADXL362 Digital Output MEMS Accelerometer example

The ADXL362 always provides 12-bit output resolution; 8-bit formatted data is also provided for more efficient single-byte transfers when a lower resolution is sufficient. Measurement ranges of ±2 g, ±4 g, and ±8 g are available, with a resolution of 1 mg/LSB on the ±2 g range. For applications where a noise level lower than the normal 550 µg/√Hz of the ADXL362 is desired, either of two lower noise modes (down to 175 µg/√Hz typical) can be selected at minimal increase in supply current.

In addition to its ultralow power consumption, the ADXL362 has many features to enable true system level power reduction. It includes a deep multimode output FIFO, a built-in micropower temperature sensor, and several activity detection modes including adjustable threshold sleep and wake-up operation that can run as low as 270 nA at a 6 Hz (approximate) measurement rate. A pin output is provided to directly control an external switch when activity is detected, if desired. In addition, the ADXL362 has provisions for external control of sampling time and/or an external clock.

The ADXL362 operates on a wide 1.6 V to 3.5 V supply range, and can interface, if necessary, to a host operating on a separate, lower supply voltage. The ADXL362 is available in a 3 mm × 3.25 mm × 1.06 mm package.

Connection

Arduino Pin Number ADXL362 Breakout
VCC VS and VIO
GND GND
10 CS
11 MOSI
12 MISO
13 SCLK

 

Code

This example requires the library from https://github.com/annem/ADXL362

#include <SPI.h>
#include <ADXL362.h>
 
ADXL362 xl;
 
int16_t temp;
int16_t XValue, YValue, ZValue, Temperature;
 
void setup(){
 
  Serial.begin(9600);
  xl.begin(10);                   // Setup SPI protocol, issue device soft reset
  xl.beginMeasure();              // Switch ADXL362 to measure mode  
 
  Serial.println("Start Demo: Simple Read");
}
 
void loop(){
 
  // read all three axis in burst to ensure all measurements correspond to same sample time
  xl.readXYZTData(XValue, YValue, ZValue, Temperature);  
  Serial.print("XVALUE=");
  Serial.print(XValue);	 
  Serial.print("\tYVALUE=");
  Serial.print(YValue);	 
  Serial.print("\tZVALUE=");
  Serial.print(ZValue);	 
  Serial.print("\tTEMPERATURE=");
  Serial.println(Temperature);	 
  delay(100);                // Arbitrary delay to make serial monitor easier to observe
}

 

Links

http://www.analog.com/en/products/sensors-mems/accelerometers/adxl362.html

ADXL362 3-Axis Digital Accelerometer Accel Sensor Module SPI for Arduino

Share

OPT3001 Digital Ambient Light Sensor and Arduino example

In this example we will connect an OPT3001 to an Arduino

The OPT3001 is a sensor that measures the intensity of visible light. The spectral response of the sensor tightly matches the photopic response of the human eye and includes significant infrared rejection.

The OPT3001 is a single-chip lux meter, measuring the intensity of light as visible by the human eye. The precision spectral response and strong IR rejection of the device enables the OPT3001 to accurately meter the intensity of light as seen by the human eye regardless of light source. The strong IR rejection also aids in maintaining high accuracy when industrial design calls for mounting the sensor under dark glass for aesthetics. The OPT3001 is designed for systems that create light-based experiences for humans, and an ideal preferred replacement for photodiodes, photoresistors, or other ambient light sensors with less human eye matching and IR rejection.

Measurements can be made from 0.01 lux up to 83k lux without manually selecting full-scale ranges by using the built-in, full-scale setting feature. This capability allows light measurement over a 23-bit effective dynamic range.

The digital operation is flexible for system integration. Measurements can be either continuous or single-shot. The control and interrupt system features autonomous operation, allowing the processor to sleep while the sensor searches for appropriate wake-up events to report via the interrupt pin. The digital output is reported over an I2C- and SMBus-compatible, two-wire serial interface.

 

Features

  • Precision Optical Filtering to Match Human Eye:
    • Rejects > 99% (typ) of IR
  • Automatic Full-Scale Setting Feature Simplifies Software and Ensures Proper Configuration
  • Measurements: 0.01 lux to 83 k lux
  • 23-Bit Effective Dynamic Range With
    Automatic Gain Ranging
  • 12 Binary-Weighted Full-Scale Range Settings:
    < 0.2% (typ) Matching Between Ranges
  • Low Operating Current: 1.8 µA (typ)
  • Operating Temperature Range: –40°C to +85°C
  • Wide Power-Supply Range: 1.6 V to 3.6 V
  • 5.5-V Tolerant I/O

Connection

 

Arduino  CJMCU-3001
5v Vcc
Gnd Gnd
SDA SDA
SCL SCL

Code

This example uses the following library https://github.com/closedcube/ClosedCube_OPT3001_Arduino

/*
 
This is example for ClosedCube OPT3001 Digital Ambient Light Sensor breakout board 
 
Initial Date: 02-Dec-2015
 
Hardware connections for Arduino Uno:
VDD to 3.3V DC
SDA to A4
SCL to A5
GND to common ground
 
Written by AA for ClosedCube
 
MIT License
 
*/
 
#include <Wire.h>
#include <ClosedCube_OPT3001.h>
 
ClosedCube_OPT3001 opt3001;
 
#define OPT3001_ADDRESS 0x44
 
void setup()
{
	Serial.begin(9600);
	Serial.println("ClosedCube OPT3001 Arduino Test");
 
	opt3001.begin(OPT3001_ADDRESS);
	Serial.print("OPT3001 Manufacturer ID");
	Serial.println(opt3001.readManufacturerID());
	Serial.print("OPT3001 Device ID");
	Serial.println(opt3001.readDeviceID());
 
	configureSensor();
	printResult("High-Limit", opt3001.readHighLimit());
	printResult("Low-Limit", opt3001.readLowLimit());
	Serial.println("----");
}
 
void loop()
{
	OPT3001 result = opt3001.readResult();
	printResult("OPT3001", result);
	delay(500);
}
 
void configureSensor() {
	OPT3001_Config newConfig;
 
	newConfig.RangeNumber = B1100;	
	newConfig.ConvertionTime = B0;
	newConfig.Latch = B1;
	newConfig.ModeOfConversionOperation = B11;
 
	OPT3001_ErrorCode errorConfig = opt3001.writeConfig(newConfig);
	if (errorConfig != NO_ERROR)
		printError("OPT3001 configuration", errorConfig);
	else {
		OPT3001_Config sensorConfig = opt3001.readConfig();
		Serial.println("OPT3001 Current Config:");
		Serial.println("------------------------------");
 
		Serial.print("Conversion ready (R):");
		Serial.println(sensorConfig.ConversionReady,HEX);
 
		Serial.print("Conversion time (R/W):");
		Serial.println(sensorConfig.ConvertionTime, HEX);
 
		Serial.print("Fault count field (R/W):");
		Serial.println(sensorConfig.FaultCount, HEX);
 
		Serial.print("Flag high field (R-only):");
		Serial.println(sensorConfig.FlagHigh, HEX);
 
		Serial.print("Flag low field (R-only):");
		Serial.println(sensorConfig.FlagLow, HEX);
 
		Serial.print("Latch field (R/W):");
		Serial.println(sensorConfig.Latch, HEX);
 
		Serial.print("Mask exponent field (R/W):");
		Serial.println(sensorConfig.MaskExponent, HEX);
 
		Serial.print("Mode of conversion operation (R/W):");
		Serial.println(sensorConfig.ModeOfConversionOperation, HEX);
 
		Serial.print("Polarity field (R/W):");
		Serial.println(sensorConfig.Polarity, HEX);
 
		Serial.print("Overflow flag (R-only):");
		Serial.println(sensorConfig.OverflowFlag, HEX);
 
		Serial.print("Range number (R/W):");
		Serial.println(sensorConfig.RangeNumber, HEX);
 
		Serial.println("------------------------------");
	}
 
}
 
void printResult(String text, OPT3001 result) {
	if (result.error == NO_ERROR) {
		Serial.print(text);
		Serial.print(": ");
		Serial.print(result.lux);
		Serial.println(" lux");
	}
	else {
		printError(text,result.error);
	}
}
 
void printError(String text, OPT3001_ErrorCode error) {
	Serial.print(text);
	Serial.print(": [ERROR] Code #");
	Serial.println(error);
}

Output

Open the serial monitor and you should see something like this

——————————
High-Limit: 83865.54 lux
Low-Limit: 0.00 lux
—-
OPT3001: 248.16 lux
OPT3001: 19.08 lux
OPT3001: 12.85 lux
OPT3001: 18.91 lux
OPT3001: 230.24 lux
OPT3001: 218.64 lux
OPT3001: 15.07 lux
OPT3001: 53.40 lux
OPT3001: 173.28 lux
OPT3001: 145.44 lux

Link

OPT3001 CJMCU-3001 ambient light sensor eye like measurement light intensity single chip illumination meter

Share

Arduino and US – 100 ultrasonic ranging module serial data mode example

US – 100 ultrasonic ranging module can be realized 2cm ~ 450cm of non-contact distance measurement function, with 2.4V to 4.5V wide voltage input range, the static function GPIO, serial variety of communication methods, within the band watchdog, stable and reliable.

This module is improved, the module itself has temperature compensation, the accuracy of the highest module now clean out treasure, absolute stable, economical, practical 20 * 45 mm module circuit 15 mm thick
US-100 ultrasonic ranging module can realize 2 cm ~ 4.5 m non-contact ranging function, with a 2.4 ~ 5.5 V of wide voltage input range, static power consumption less than 2 mA, bring their own temperature sensor calibration on the ranging results, and has GPIO, serial ports and so on many kinds of communication mode, inside take watchdog, stable and reliable.

Specification:

Voltage: DC 2.4V – 5V
Quiescent current: < 2mA
Output mode: level or UART
Induction angle: < 15°
Detection distance: 2cm – 450cm
Precision: 0.3cm ± 1%
Working temperature: -20°C to +70°C
Size: 45 x 20 x 1.6mm

 

1. VCC: 5V DC
2. Trig: trigger input
3. Echo: pulse output
4. GND: ground
5. GND: ground

 

Connection

Module Connection Arduino Connection
VCC 5v
GND GND
Trig TX (Pin 1)
Echo RX (Pin 0)

 

Code

// RX (Pin 0 of Arduino digital IO) to Echo
// TX (Pin 1 of Arduino digital IO) to Trig 
unsigned int HighLen = 0;
unsigned int LowLen  = 0;
unsigned int Len_mm  = 0;
 
void setup() 
{   
    Serial.begin(9600);                         
}
 
void loop() 
{
    Serial.flush();                               
    Serial.write(0X55);                           // trig US-100 begin to measure the distance
    delay(500);                                   
    if(Serial.available() >= 2)                   // receive 2 bytes 
    {
        HighLen = Serial.read();                   // High byte of distance
        LowLen  = Serial.read();                   // Low byte of distance
        Len_mm  = HighLen*256 + LowLen;            // Calculate the distance
        if((Len_mm > 1) && (Len_mm < 10000))       // normal distance should between 1mm and 10000mm (1mm, 10m)
        {
            Serial.print("Present Length is: ");   
            Serial.print(Len_mm, DEC);             
            Serial.println("mm");                  
        }
    }
    delay(500);                                   
}

 

Output

UUUPresent Length is: 58mm
UPresent Length is: 57mm
UPresent Length is: 58mm
UPresent Length is: 58mm
UUUUPresent Length is: 53mm
UPresent Length is: 57mm
UUUUPresent Length is: 78mm

 

Link

1 pc US-100 Ultrasonic Sensor Module With Temperature Compensation Range Distance 450cm For Arduino

Share

Arduino and MMA8653 example

The NXP® MMA8653FC 10-bit accelerometer has industry leading performance in a small DFN package. Packed with embedded functions that include:

Flexible user-programmable options and two configurable interrupt pins
Overall power savings through inertial wake-up interrupt signals that monitor events and remain in a low-power mode during periods of inactivity

Features
Low-profile 2 x 2 x 1.0 mm DFN package
High sensitivity: 1 mg per LSB
Low noise: 150 micro g per root Hertz (independent of resolution)
Low-power mode: 7 micro amps
Interrupt and IIC interface supply: 1.62 to 3.6 V
Supply voltage : 1.95 to 3.6 V
Output data rate: 1.5 to 800 Hz

Code

 

#include <Wire.h>
 
// I2C BUS: already defined in "wire" librairy
// SDA: PIN 2 with pull up 4.7K to 3.3V on arduino Micro
// SCL: PIN 3 with pull up 4.7K to 3.3V on arduino Micro
// Accelerometer connected to +3.3V of arduino DO NOT CONNECT TO 5V (this will destroy the accelerometer!)
// all GND Pin of accelerometer connected to gnd of arduino
 
/********************ACCELEROMETER DATAS************/
// adresss of accelerometer
int adress_acc=0X1D; // MMA8653FC and MMA8652FC
// adress of registers for MMA8653FC
int ctrl_reg1 = 0x2A;
int ctrl_reg2 = 0x2B;
int ctrl_reg3 = 0x2C;
int ctrl_reg4 = 0x2D;
int ctrl_reg5 = 0x2E;
int int_source = 0x0C;
int status_ = 0x00;
int f_setup = 0x09;
int out_x_msb = 0x01;
int out_y_msb = 0x03;
int out_z_msb = 0x05;
int sysmod = 0x0B;
int xyz_data_cfg = 0x0E;
 
/******PROGRAM DATAS**********/
int result [3];
int axeXnow ;
int axeYnow ;
int axeZnow ;
 
 
void setup(){
Wire.begin(); // start of the i2c protocol
Serial.begin(9600); // start serial for output
ACC_INIT(); // initialize the accelerometer by the i2c bus. enter the sub to adjust the range (2g, 4g, 8g), and the data rate (800hz to 1,5Hz)
}
 
//------------------------------------------------------------------
 
void loop()
{
I2C_READ_ACC(0x00);
Serial.print(axeXnow); 
Serial.print(";");
Serial.print(axeYnow); 
Serial.print(";");
Serial.print(axeZnow); 
Serial.print(";");
Serial.println();
delay(500);
}
 
//------------------------------------------------------------------
 
void ACC_INIT()
{
I2C_SEND(ctrl_reg1 ,0X00); // standby to be able to configure
delay(10);
 
I2C_SEND(xyz_data_cfg ,B00000000); // 2G full range mode
delay(1);
// I2C_SEND(xyz_data_cfg ,B00000001); // 4G full range mode
// delay(1);
// I2C_SEND(xyz_data_cfg ,B00000010); // 8G full range mode
// delay(1);
 
I2C_SEND(ctrl_reg1 ,B00000001); // Output data rate at 800Hz, no auto wake, no auto scale adjust, no fast read mode
delay(1);
// I2C_SEND(ctrl_reg1 ,B00100001); // Output data rate at 200Hz, no auto wake, no auto scale adjust, no fast read mode
// delay(1);
// I2C_SEND(ctrl_reg1 ,B01000001); // Output data rate at 50Hz, no auto wake, no auto scale adjust, no fast read mode
// delay(1);
// I2C_SEND(ctrl_reg1 ,B01110001); // Output data rate at 1.5Hz, no auto wake, no auto scale adjust, no fast read mode
// delay(1); 
}
 
//------------------------------------------------------------------
 
void I2C_SEND(unsigned char REG_ADDRESS, unsigned char DATA) //SEND data to MMA7660
{
 
Wire.beginTransmission(adress_acc);
Wire.write(REG_ADDRESS);
Wire.write(DATA);
Wire.endTransmission();
}
 
//------------------------------------------------------------------
 
void I2C_READ_ACC(int ctrlreg_address) //READ number data from i2c slave ctrl-reg register and return the result in a vector
{
byte REG_ADDRESS[7];
int accel[4];
int i=0; 
Wire.beginTransmission(adress_acc); //=ST + (Device Adress+W(0)) + wait for ACK
Wire.write(ctrlreg_address); // store the register to read in the buffer of the wire library
Wire.endTransmission(); // actually send the data on the bus -note: returns 0 if transmission OK-
Wire.requestFrom(adress_acc,7); // read a number of byte and store them in wire.read (note: by nature, this is called an "auto-increment register adress")
 
for(i=0; i<7; i++) // 7 because on datasheet p.19 if FREAD=0, on auto-increment, the adress is shifted
{
REG_ADDRESS[i]=Wire.read(); //each time you read the write.read it gives you the next byte stored. The couter is reset on requestForm
}
 
for (i=1;i<7;i=i+2)
{
accel[0] = (REG_ADDRESS[i+1]|((int)REG_ADDRESS[i]<<8))>>6; // X
if (accel[0]>0x01FF) {accel[1]=(((~accel[0])+1)-0xFC00);} // note: with signed int, this code is optional
else {accel[1]=accel[0];} // note: with signed int, this code is optional
switch(i){
case 1: axeXnow=accel[1];
break;
case 3: axeYnow=accel[1];
break;
case 5: axeZnow=accel[1];
break;
}
}
 
}
 
//------------------------------------------------------------------
 
void I2C_READ_REG(int ctrlreg_address) //READ number data from i2c slave ctrl-reg register and return the result in a vector
{
unsigned char REG_ADDRESS;
int i=0; 
Wire.beginTransmission(adress_acc); //=ST + (Device Adress+W(0)) + wait for ACK
Wire.write(ctrlreg_address); // register to read
Wire.endTransmission();
Wire.requestFrom(adress_acc,1); // read a number of byte and store them in write received
}

 

 

Output

 

107;-65281;191;
-65488;511;89;
-65387;15;87;
-65491;511;-65443;
-65364;-65423;-65470;
229;74;-65269;
237;-65511;64;
-65355;-65347;73;
-65360;132;14;
-65481;-65263;-65323;
-65453;228;-65448;
-65354;94;215;
55;-65464;276;

Links

Share