Categories

Follow Us

Arduino and MCP3424 A/D converter example

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

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 Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
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

2.0479843616
2.0479843616
2.0479843616
0.0402031278
0.0402031278
0.0401875019
0.0401875019
0.0401875019
0.0401875019
0.0401875019
0.0401875019
0.0401718759
0.0401406240
1.9360313415
2.0479843616
2.0479843616

Share

LIS3DSH accelerometer sensor and Arduino example

In this example we connect a LIS3DSH sensor to an Arduino. Lets look at the sensor

The LIS3DSH is an ultra-low-power high-performance three-axis linear accelerometer belonging to the “nano” family with an embedded state machine that can be programmed to implement autonomous applications.

The LIS3DSH has dynamically selectable full scales of ±2g/±4g/±6g/±8g/±16g and is capable of measuring accelerations with output data rates from 3.125 Hz to 1.6 kHz. The self-test capability allows the user to check the functioning of the sensor in the final application. The device can be configured to generate interrupt signals activated by user-defined motion patterns.

The LIS3DSH has an integrated first-in, first-out (FIFO) buffer allowing the user to store data in order to limit intervention by the host processor. The LIS3DSH is available in a small thin plastic land grid array package (LGA) and is guaranteed to operate over an extended temperature range from -40 °C to +85 °C.

Key Features

  • Wide supply voltage, 1.71 V to 3.6 V
  • Independent IOs supply (1.8 V) and supply voltage compatible
  • Ultra-low power consumption
  • ±2g/±4g/±6g/±8g/±16g dynamically selectable full scale
  • I2C/SPI digital output interface
  • 16-bit data output
  • Programmable embedded state machines
  • Embedded temperature sensor
  • Embedded self-test
  • Embedded FIFO
  • 10000 g high shock survivability

 

Parts List

Part Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
LIS3DSH module CJMCU- LIS3DSH High-resolution Three-axis Accelerometer Triaxial Accelerometer Module LIS3DH
Connecting Wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Connection

An easy device to connect – remember its 3.3v

Arduino and LIS3DSH

Arduino and LIS3DSH

 

Code

You need to download and install the https://github.com/yazug/LIS3DSH library

#include <Wire.h>
#include <LIS3DSH.h>
 
LIS3DSH accel;
 
void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  accel.enableDefault();
}
 
void loop() 
{
  int16_t x, y, z;
  int8_t temperature;
 
  accel.readAccel(&x, &y, &z);
  accel.readTemperature(&temperature);
 
  Serial.print("Accel ");
  Serial.print("X: ");
  Serial.print(x);
  Serial.print(" Y: ");
  Serial.print(y);
  Serial.print(" Z: ");
  Serial.print(z);
  Serial.print(" T: ");
  Serial.println(temperature);
 
  delay(100);
}

 

Output

Open the serial monitor and you should see something like this – move the module around

Accel X: -16041 Y: 25096 Z: 30902 T: -5
Accel X: -4264 Y: -3219 Z: -4920 T: -5
Accel X: -3665 Y: -24195 Z: -6582 T: -5
Accel X: -2387 Y: 29273 Z: 25093 T: -5
Accel X: -10046 Y: 18720 Z: 15595 T: -5
Accel X: -19708 Y: -2226 Z: 5157 T: -6
Accel X: -13490 Y: -2875 Z: -2190 T: -6
Accel X: -10057 Y: 11695 Z: 22556 T: -6
Accel X: 1924 Y: 27351 Z: 16082 T: -5
Accel X: -4231 Y: 18639 Z: 14947 T: -5
Accel X: -17774 Y: -2564 Z: 6430 T: -5
Accel X: -13527 Y: -7457 Z: 5894 T: -5
Accel X: -20130 Y: -15443 Z: 10049 T: -5
Accel X: 3386 Y: -1488 Z: 17183 T: -5
Accel X: -2989 Y: 15951 Z: 9403 T: -6
Accel X: -15118 Y: 9226 Z: 6249 T: -6

 

Link

 

Share

Arduino and PCF8575 I/O expander examples

This 16-bit I/O expander for the two-line bidirectional bus (I2C) is designed for 2.5-V to 5.5-V VCC operation.

The PCF8575 device provides general-purpose remote I/O expansion for most microcontroller families by way of the I2C interface [serial clock (SCL), serial data (SDA)].

The device features a 16-bit quasi-bidirectional input/output (I/O) port (P07–P00, P17–P10), including latched outputs with high-current drive capability for directly driving LEDs. Each quasi-bidirectional I/O can be used as an input or output without the use of a data-direction control signal. At power on, the I/Os are high. In this mode, only a current source to VCC is active.

Features

  • I2C to Parallel-Port Expander
  • Open-Drain Interrupt Output
  • Low Standby-Current Consumption of 10 µA Max
  • Compatible With Most Microcontrollers
  • 400-kHz Fast I2C Bus
  • Address by Three Hardware Address Pins for Use of up to Eight Devices
  • Latched Outputs With High-Current Drive Capability for Directly Driving LEDs

 

Connection

This example shows only one LED

Arduino and PCF8575

Arduino and PCF8575

 

Parts List

Part Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
PCF8575 PCF8575 IIC I2C I/O Extension Shield Module 16 bit SMBus I/O

 

Code examples

Output – switch LED on and off

#include <Wire.h>
 
// Set I2C address
int address = 0x20;
 
void setup()
{ 
  Wire.begin();
  // Set all ports as output
  pf575_write(word(B11111111,B11111111));
} 
 
void loop()
{
  // Set port P0 on
  pf575_write(word(B00000000,B00000001));
  delay(1000);
  // Set port P0 off
  pf575_write(word(B00000000,B00000000));
  delay(1000);
} 
 
// Function for writing two Bytes to the I2C expander device
void pf575_write(uint16_t data) 
{
  Wire.beginTransmission(address);
  Wire.write(lowByte(data));
  Wire.write(highByte(data));
  Wire.endTransmission();
}

 

Output – switch LEDs on and off

#include <Wire.h>
 
byte address = 0x20;   // address of PCF8575
 
void setup()
{
   Wire.begin();       // join i2c bus as master
}
 
 
void loop()
{
  unsigned char x;
  unsigned char y;
 
  for (x=0, y=255; (x+y)==255; x++, y--)
  {
      Wire.beginTransmission(address);   // send the address and the write cmnd
      Wire.write(x);                      // pack the first byte
      Wire.write(y);                      // pack the second byte
      Wire.endTransmission();            // send the data
      delay(150);     
   }
}
Share

Arduino and APDS-9960 sensor example

In this example we will connect a APDS-9960 sensor to an Arduino Uno

Lets look at some information about the APDS-9960

The Broadcom APDS-9960 is a digital RGB, ambient light, proximity and gesture sensor device in a single 8-pin package. The device has an I2C compatible interface providing red, green, blue, clear (RGBC), proximity and gesture sensing with IR LED. The RGB and ambient light sensing feature detects light intensity under various lighting conditions and through various attentuation materials including darkened glass. In addition, the integrated UV-IR blocking filter enables accurate ambient light and correlated color temperature sensing.

The proximity and gesture feature is factory-trimmed and calibrated to 100mm proximity detection distance without requiring customer calibrations. Gesture detection utilizes four directional photodiodes, integrated with visible blocking filter, to accurately sense simple UP-DOWN-RIGHT-LEFT gestures or more complex gestures. The addition of micro-optics lenses within the module provides high efficient transmission and reception of infrared energy. An internal state machine allows the device to be put into a low power state between RGBC, proximity and gesture measurements providing very low power consumption.

Connection

 

Arduino and APDS-9960

Arduino and APDS-9960

Parts List

Part Link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
Dupont cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
APDS-9960 sensor (cjmcu-88) APDS-9960 proximity detection and non-contact gesture detection RGB and Gesture

Code

This is the gesture example from the sparkfun library – https://github.com/sparkfun/APDS-9960_RGB_and_Gesture_Sensor

#include <Wire.h>
#include <SparkFun_APDS9960.h>
 
// Pins
#define APDS9960_INT    2 // Needs to be an interrupt pin
 
// Constants
 
// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
 
void setup() {
 
  // Set interrupt pin as input
  pinMode(APDS9960_INT, INPUT);
 
  // Initialize Serial port
  Serial.begin(9600);
  Serial.println();
  Serial.println(F("--------------------------------"));
  Serial.println(F("SparkFun APDS-9960 - GestureTest"));
  Serial.println(F("--------------------------------"));
 
  // Initialize interrupt service routine
  attachInterrupt(0, interruptRoutine, FALLING);
 
  // Initialize APDS-9960 (configure I2C and initial values)
  if ( apds.init() ) {
    Serial.println(F("APDS-9960 initialization complete"));
  } else {
    Serial.println(F("Something went wrong during APDS-9960 init!"));
  }
 
  // Start running the APDS-9960 gesture sensor engine
  if ( apds.enableGestureSensor(true) ) {
    Serial.println(F("Gesture sensor is now running"));
  } else {
    Serial.println(F("Something went wrong during gesture sensor init!"));
  }
}
 
void loop() {
  if( isr_flag == 1 ) {
    detachInterrupt(0);
    handleGesture();
    isr_flag = 0;
    attachInterrupt(0, interruptRoutine, FALLING);
  }
}
 
void interruptRoutine() {
  isr_flag = 1;
}
 
void handleGesture() {
    if ( apds.isGestureAvailable() ) {
    switch ( apds.readGesture() ) {
      case DIR_UP:
        Serial.println("UP");
        break;
      case DIR_DOWN:
        Serial.println("DOWN");
        break;
      case DIR_LEFT:
        Serial.println("LEFT");
        break;
      case DIR_RIGHT:
        Serial.println("RIGHT");
        break;
      case DIR_NEAR:
        Serial.println("NEAR");
        break;
      case DIR_FAR:
        Serial.println("FAR");
        break;
      default:
        Serial.println("NONE");
    }
  }
}

 

Output

Here is the output that I saw in the serial monitor

APDS-9960 initialization complete
Gesture sensor is now running
UP
NEAR
FAR
FAR
UP
NONE
NONE
NEAR
NEAR
LEFT
NONE
RIGHT
LEFT
NONE
LEFT
RIGHT

 

Link

https://docs.broadcom.com/docs/AV02-4191EN

Share