Microbit and MAG3110 magnetic sensor example

The MAG3110 is a small, low-power digital 3D magnetic sensor with a wide dynamic range to allow operation in PCBs with high extraneous magnetic fields. The MAG3110:

  • Measures the components of the local magnetic field, the sum of the geomagnetic field and the magnetic field created by components on the circuit board
  • Can be used in conjunction with a 3-axis accelerometer so that orientation-independent accurate compass heading information may be achieved
  • Features a standard I²C serial interface and is capable of measuring local magnetic fields up to 10 Gauss with output data rates up to 80 Hz

 

Features

Layout

 

microbit and MAG3110

microbit and MAG3110

 

Code

 

#include <Wire.h>
#define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't change
 
void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600); // start serial for output
  config(); // turn the MAG3110 on
}
 
void loop()
{
  print_values();
  delay(500);
}
 
void config(void)
{
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x11); // cntrl register2
  Wire.write(0x80); // send 0x80, enable auto resets
  Wire.endTransmission(); // stop transmitting
  delay(15);
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x10); // cntrl register1
  Wire.write(1); // send 0x01, active mode
  Wire.endTransmission(); // stop transmitting
}
 
void print_values(void)
{
  Serial.print("x=");
  Serial.print(readx());
  Serial.print(",");
  Serial.print("y=");
  Serial.print(ready());
  Serial.print(",");
  Serial.print("z=");
  Serial.println(readz());
}
 
int readx(void)
{
  int xl, xh; //define the MSB and LSB
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x01); // x MSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    xh = Wire.read(); // receive the byte
  }
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x02); // x LSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    xl = Wire.read(); // receive the byte
  }
  int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
  return xout;
}
 
int ready(void)
{
  int yl, yh; //define the MSB and LSB
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x03); // y MSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    yh = Wire.read(); // receive the byte
  }
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x04); // y LSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    yl = Wire.read(); // receive the byte
  }
  int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
  return yout;
}
 
int readz(void)
{
  int zl, zh; //define the MSB and LSB
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x05); // z MSB reg
  Wire.endTransmission(); // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
    zh = Wire.read(); // receive the byte
  }
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.write(0x06); // z LSB reg
  Wire.endTransmission(); // stop transmitting
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
 
  while(Wire.available()) // slave may send less than requested
  {
   zl = Wire.read(); // receive the byte
  }
  int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
  return zout;
}

 

Output

Open the serial monitor window

x=126,y=1552,z=72
x=256,y=1552,z=19
x=259,y=15,z=67
x=124,y=1544,z=67
x=128,y=1545,z=64
x=124,y=1544,z=68
x=0,y=1953,z=513
x=50,y=2082,z=88
x=81,y=1698,z=612
x=186,y=1961,z=65056
x=0,y=1188,z=13
x=258,y=1286,z=65041
x=112,y=1536,z=65113

 

Links

MAG3110 Triple 3 Axis Magnetometer Breakout Electronic Compass Sensor Module For Arduino

Microbit and MMA7660 accelerometer example

The MMA7660FC is a digital output I²C, very low-power, low-profile capacitive micro-machined accelerometer featuring a low pass filter, compensation for zero-g offset and gain errors and conversion to six-bit digital values at a user configurable output data rate. The device can be used for sensor data changes, product orientation and gesture detection through an interrupt pin (INT).

Communication is handled through a 2 pin I2C interface, available on a wide range of microcontrollers. The I2C address by default is 0x4c.

Features

  • Digital output I²C
  • 3 mm x 3 mm x 0.9 mm DFN package
  • Low-power current consumption
    • Off mode: 0.4 µA
    • Standby mode: 2 µA
    • Active mode: Configurable down to 47 µA
  • Low-voltage operation: 2.4 – 3.6-volts
  • 3-axis ±1.5 g MEMS sensor and CMOS interface controller built into one package
  • Configurable output data rate from one to 120 samples a second
  • Auto wake/sleep feature for low-power consumption
  • Tilt orientation detection for portrait/landscape capability
  • Gesture detection including shake and pulse detection
  • Robust design, high shock survivability (10,000g)

 

Connection

Module connection to Microbit

  • GND = GND
  • VCC = 3v3
  • SDA = 20
  • SCL = 21

 

Code

I used this library – https://github.com/mcauser/Grove-3Axis-Digital-Accelerometer-1.5g-MMA7660FC

This is the default example

 

#include <Wire.h>
#include "MMA7660.h"
MMA7660 acc;
void setup()
{
acc.init();
Serial.begin(115200);
}
void loop()
{
static long cnt = 0;
static long cntout = 0;
float ax,ay,az;
int8_t x, y, z;
acc.getXYZ(&x,&y,&z);
if(acc.getAcceleration(&ax,&ay,&az))
{
Serial.print("got data ok: ");
}
else
{
Serial.print("timed out: ");
}
Serial.println("acceleration of X/Y/Z: ");
Serial.print(ax);
Serial.println(" g");
Serial.print(ay);
Serial.println(" g");
Serial.print(az);
Serial.println(" g");
Serial.println();
delay(500);
}

 

Output

Open the serial monitor

got data ok: acceleration of X/Y/Z:
2.76 g
0.19 g
2.33 g

got data ok: acceleration of X/Y/Z:
1.48 g
1.48 g
1.52 g

got data ok: acceleration of X/Y/Z:
2.62 g
0.00 g
0.48 g

got data ok: acceleration of X/Y/Z:
2.90 g
0.81 g
2.43 g

 

Link

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

1PCS NEW MMA7660 Replace MMA7260 3 Axis Triaxial accelerometer sensor module

micro:bit and OLED display example

micro:bit and OLED display example

This example uses an OLED display these typically come in a couple of different sizes 128×32 and 128×64, this particular example will use the I2C connection from the Micro:bit to the display. There are a couple of libraries that make life easier.

Lets look at a typical oled display

These will come in useful for various projects for example displaying the date and time or maybe temperature readings from a sensor

Connection

Pin Label Microbit PIN I2C Function Notes
GND Ground Ground 0V
VCC Power Power Regulated 5V supply.
SDA SDA / P20 SDA Serial data in
SCL SCL / P19 SCL  I2C clock

This layout shows a 128×32 connected to the LOLIN32, 128×64 I2C devices would be the same

 

microbit and oled

microbit and oled

 

Code

This example uses the https://github.com/adafruit/Adafruit_SSD1306/archive/master.zip and https://github.com/adafruit/Adafruit-GFX-Library/archive/master.zip , there are several built in examples. I have modified one just to display text as further examples will write text to a display

 

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
 
Adafruit_SSD1306 display(OLED_RESET);
 
void setup()   
{                  
  Serial.begin(9600);   
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)  // init done  
  display.clearDisplay();  // text display tests  
  display.setTextSize(1);  
  display.setTextColor(WHITE);  
  display.setCursor(0,0);  
  display.println("Hello, world!");  
  display.setTextColor(BLACK, WHITE); // &#039;inverted&#039; text  
  display.println(3.141592);  
  display.setTextSize(2);  
  display.setTextColor(WHITE);  
  display.print("0x");   
  display.println(0xDEADBEEF, HEX);  
  display.display();  
  display.clearDisplay();
}
 
void loop() 
{  
}

 

Link

1Pcs 128X64 OLED LCD LED Display Module For Arduino 0.96″ I2C IIC SPI Serial new original

Microbit and HDC1080 humidity and temperature sensor example

The HDC1080 is a digital humidity sensor with integrated temperature sensor that provides excellent measurement accuracy at very low power. The HDC1080 operates over a wide supply range, and is a low cost, low power alternative to competitive solutions in a wide range of common applications. The humidity and temperature sensors are factory calibrated.

Features
Relative Humidity Accuracy ±2% (typical)
Temperature Accuracy ±0.2°C (typical)
Excellent Stability at High Humidity
14 Bit Measurement Resolution
100 nA Sleep Mode Current

 

Connection

 

 MIcrobit connection  Module connection
 3v3 3v3
 GND  GND
 SDA – 20  SDA
 SCL – 21  SCL

 

Code

I needed to modify the   https://github.com/closedcube/ClosedCube_HDC1080_Arduino library, otherwise there was compilation errors

You need to change Wire.write(0x00); to Wire.write((byte)0x00); – Modified HDC1080 library

This is the default example

 

#include <Wire.h>
#include "ClosedCube_HDC1080.h"
 
ClosedCube_HDC1080 hdc1080;
 
void setup()
{
Serial.begin(9600);
Serial.println("ClosedCube HDC1080 Arduino Test");
 
// Default settings:
// - Heater off
// - 14 bit Temperature and Humidity Measurement Resolutions
hdc1080.begin(0x40);
 
Serial.print("Manufacturer ID=0x");
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
Serial.print("Device ID=0x");
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
 
printSerialNumber();
 
}
 
void loop()
{
Serial.print("T=");
Serial.print(hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(hdc1080.readHumidity());
Serial.println("%");
delay(3000);
}
 
void printSerialNumber() {
Serial.print("Device Serial Number=");
HDC1080_SerialNumber sernum = hdc1080.readSerialNumber();
char format[12];
sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);
Serial.println(format);
}

 

 

 

Output

Open the serial monitor window and you should expect to see something like this

ClosedCube HDC1080 Arduino Test
Manufacturer ID=0x54T=23.06C, RH=51.07%
T=23.06C, RH=51.07%
T=23.08C, RH=51.05%
T=23.08C, RH=51.15%
T=23.10C, RH=51.15%
T=23.11C, RH=51.15%
T=23.11C, RH=51.05% ClosedCube_HDC1080_Arduino-master

 

Links

1PC 2.7 V to 5.5 V HDC1080 high precision temperature and humidity sensor humidity temperature module

http://www.ti.com/lit/gpn/hdc1080

http://www.ti.com/lit/pdf/snau189