0

Example for MRMS refl. sens. 9x, CAN, anal, I2C, visible light (mrm-ref-vis9), using analog values

Prerequisites

Required hardware:

Task

We will connect the sensor using I2C interface.

Hookup

  1. Do not connect battery yet.
  2. Use ML-R Cable KK254-KK254 20 cm (mrm-kk2.54-2.54-20) and connect regulator's 5 V voltage output to sensor's voltage input (red connector). If You have some other power supply, use 2 Dupont 0.1" wires to deliver 5 V DC to the red connector. Mind + and -! The sensor has reverse input voltage protection, but it is better not to test it. At least, the sensor will not work, if voltage is reversed. If You power the microcontroller card using the same regulator, You will have to rework the wires to supply the both cards.
  3. Connecting 5 V voltage output to Mega 2560 "5V" input, like in the picture, will deliver voltage to Mega 2560 internal linear regulator's output, skipping Mega 2560 protection, but MRMS Power Supply 3x (mrm-ps3x) has the protection built-in. Doing so will replace an inefficient linear regulator with a switching step-down regulator. If You do not have 5 V DC voltage source, You can connect 7 - 12 V (battery output) directly to Mega 2560 pin "VIn".
  4. Connect sensor's I2C pins to Mega's I2C pins, using Dupont wires.
  5. Check all the polarities!
  6. Turn power switch off.
  7. Connect battery.
  8. Turn power switch on.
  9. Sensor's green LED must blink 0.1 sec. every second. If so, You are good to go.
Even if You have some other type of hookup, make sure that the sensor and Mega 2560 have common ground (GNDs connected).

Pull-up resistors

I2C demands pull-up resistors. Mega 2560 doesn't need them because it features internal 10 kΩ resistors. If You intend to use some other board, You may be forced to use external 2 - 10 kΩ pull-up resistors, connecting 3.3 V to both SCL and SDA, each with its own resistor.

Program

Here is a sample code.

#include <Wire.h>                                     // I2C library.

#define DATA_COUNT 18                                 // Number of bytes to receive.
#define I2C_ADDRESS 0x09                              // Sensor's I2C address.
#define READ_ANALOG 0x00                              // Read all the transistors and use analog values.

uint32_t ms;                                          // Variable used for displaying results periodically.

void setup() {
  Serial.begin(115200);                               // Adjust monitor speed to 115200 bps in order not to get garbage in the window.
  Wire.begin();                                       // Start I2C.
  delay(500);                                         // Wait to be sure that the text will be displayed.
  Serial.println("Start");
}

void loop() {
  if (millis() - ms > 100){                           // Every 0.1 sec...

    Wire.beginTransmission(I2C_ADDRESS);              // Start a "write" I2C command (write to sensor).
    Wire.write((uint8_t)READ_ANALOG);                 // Send the desired command.
    Wire.endTransmission();                           // End "write" part.

    Wire.requestFrom((int)I2C_ADDRESS, DATA_COUNT);   // Start "read" part (read from sensor). Request DATA_COUNT bytes of data.
    bool even = false;                                // Even byte.
    uint16_t data;
    while(Wire.available()){
      if (even){
        data = (data << 8) | Wire.read();             // Construct 16-bit value from 2 8-bit ones.
        Serial.print((int)data);
        Serial.print(" ");
      }
      else
        data = Wire.read();                           // Odd byte, wait for even.
      even = !even;
    }
    Serial.println();
    ms = millis();
  }
}
Upload the program, open Arduino monitor, and change baud rate to 115200.