Categories

About EEPROM Write and Arduino

In this example we will write to the EEPROM which is on the micro controller of the Arduino

The supported micro-controllers on the various Arduino and Genuino boards have different amounts of EEPROM: 1024 bytes on the ATmega328P, 512 bytes on the ATmega168 and ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560. The Arduino and Genuino 101 boards have an emulated EEPROM space of 1024 bytes.

This example illustrates how to store values read from analog input 0 into the EEPROM using the EEPROM.write() function. A little about EEPROM first

EEPROM (also E2PROM) stands for Electrically Erasable Programmable Read-Only Memory and is a type of non-volatile memory used in computers, integrated in microcontrollers for smart cards and remote keyless system, and other electronic devices to store relatively small amounts of data but allowing individual bytes to be erased and reprogrammed.

EEPROMs are organized as arrays of floating-gate transistors. EEPROMs can be programmed and erased in-circuit, by applying special programming signals. Originally, EEPROMs were limited to single byte operations which made them slower, but modern EEPROMs allow multi-byte page operations. It also has a limited life for erasing and reprogramming, now reaching a million operations in modern EEPROMs. In an EEPROM that is frequently reprogrammed while the computer is in use, the life of the EEPROM is an important design consideration.

Flash memory is a type of EEPROM designed for high speed and high density, at the expense of large erase blocks (typically 512 bytes or larger) and limited number of write cycles (often 10,000). There is no clear boundary dividing the two, but the term “EEPROM” is generally used to describe non-volatile memory with small erase blocks (as small as one byte) and a long lifetime (typically 1,000,000 cycles). Many microcontrollers include both: flash memory for the firmware, and a small EEPROM for parameters and history. – https://en.wikipedia.org/wiki/EEPROM

Codebender

Code

/*
 * EEPROM Write
 */
 
#include <EEPROM.h>
 
/** the current address in the EEPROM **/
int addr = 0;
 
void setup()
{
}
 
void loop() 
{
  /*
    Need to divide by 4 because analog inputs range from
    0 to 1023 and each byte of the EEPROM can only hold a value from 0 to 255.
  */
  int val = analogRead(0) / 4;
 
  /*
    Write the value to fthe EEPROM.
  */
  EEPROM.write(addr, val);
 
  /*
    Advance to the next address, when at the end restart at the beginning.
  */
  addr = addr + 1;
  if (addr == EEPROM.length()) {
    addr = 0;
  }
 
 
  delay(100);
}
Share

Arduino and BMG160 3-axis angular rate sensor example

The BMG160 is an ultra-small, digital 3-axis angular rate sensor with a measurement range up to 2000°/s and a digital resolution of 16 bit for consumer electronics applications. The BMG160 allows low-noise measurement of angular rates in 3 perpendicular axes and is designed for use in cellular phones, handhelds, computer peripherals, man-machine interfaces, virtual reality features, remote and game controllers.

With its small footprint of only 3 x 3 mm² the BMG160 is unique in the class of low-noise consumer electronics gyroscopes. The zero-rate offset and offset stability over temperature of the BMG160 are outstanding

Parameter Technical data
Digital resolution 16 bit
Measurement ranges
(programmable)
± 125 °/s, ± 250 °/s,
± 500 °/s, ± 1000 °/s,
± 2000 °/s
Sensitivity (calibrated) ± 125°/s: 262.4 LSB/°/s
± 250°/s: 131.2 LSB/°/s
± 500°/s: 65.5 LSB/°/s
± 1000°/s: 32.8 LSB/°/s
± 2000°/s: 16.4 LSB/°/s
Zero-g offset (typ., over life-time) ± 1 °/s
Zero-rate offset over temperature 0.015 °/s/K
Noise density (typ.) 0.014 °/s/√Hz
Low-pass filter bandwiths (progr.) 230, 116, 64, 47, 32,
23, 12 Hz
Date rates (programmable) 2000, 1000,
400, 200, 100 Hz
Digital inputs/outputs SPI, I²C,
2x digital interrupts
Supply voltage (VDD) 2.4 … 3.6 V
I/0 supply voltage (VDDIO) 1.2 … 3.6 V
Temperature range -40 … +85 °C
Current consumption
– full operation
– low-power mode
5.0 mA
2.5 mA
FIFO data buffer 100 samples depth
(each axis)
LGA package 3 x 3 x 0.95 mm³
Shock resistance 10,000 g x 200 μs

 

Connection

These are the only connections that are required

Arduino Connection CJ_MCU 160
5v Vcc
Gnd Gnd
SDA SDA
SCL SCL

 

Code

#include<Wire.h>
 
// BMG160 I2C address is 0x68(104)
#define Addr 0x68
 
void setup() 
{
  // Initialise I2C communication as MASTER 
  Wire.begin();
  // Initialise Serial Communication, set baud rate = 9600
  Serial.begin(9600);
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Range register
  Wire.write(0x0F);
  // Configure full scale range 2000 dps
  Wire.write(0x80);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Bandwidth register
  Wire.write(0x10);
  // Set bandwidth = 200 Hz
  Wire.write(0x04);
  // Stop I2C Transmission
  Wire.endTransmission();
  delay(300);
}
 
void loop()
{
  unsigned int data[6];
  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Gyrometer data register
  Wire.write(0x02);
  // Stop I2C Transmission
  Wire.endTransmission();
 
  // Request 6 bytes of data
  Wire.requestFrom(Addr, 6);
  // Read 6 bytes of data
  // xGyro lsb, xGyro msb, yGyro lsb, yGyro msb, zGyro lsb, zGyro msb
  if(Wire.available() == 6)
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
  }
  delay(300);
 
  // Convert the data
  int xGyro = ((data[1] * 256) + data[0]);
  int yGyro = ((data[3] * 256) + data[2]);
  int zGyro = ((data[5] * 256) + data[4]);
 
  // Output data to the serial monitor
  Serial.print("X-Axis of Rotation:  ");
  Serial.println(xGyro);
  Serial.print("Y-Axis of Rotation:  ");
  Serial.println(yGyro);
  Serial.print("Z-Axis of Rotation:  ");
  Serial.println(zGyro);
  delay(500);
}

 

Output

Open the serial monitor and you should see something like this

X-Axis of Rotation: -336
Y-Axis of Rotation: 572
Z-Axis of Rotation: -1602
X-Axis of Rotation: -158
Y-Axis of Rotation: -256
Z-Axis of Rotation: 496
X-Axis of Rotation: -4916
Y-Axis of Rotation: 2098
Z-Axis of Rotation: 1128

 

Link

CJMCU-160 Sensortec three axis gyro attitude sensor module BMG160

Share

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

WS2812 RGB led ring example on Arduino

This was a WS2812b led ring that I purchased for low cost. There are 12 LEDs in total on this and once you solder 3 cables to the module you are ready to go

Connect Vcc to Arduino 5 volts, Gnd to Gnd and the In to D2, you can change this to any pin but you will need to change the code below

In the code example below we will display red, green and blue on all the LEDs

 

Code

You will need the Adafruit Neopixel library installed for this example

#include <Adafruit_NeoPixel.h>
 
#define PIN            2
 
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      12
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
int delayval = 250; // delay for half a second
 
void setup() 
{
  pixels.begin(); // This initializes the NeoPixel library.
}
 
void loop() 
{
  for(int i=0;i<NUMPIXELS;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(255,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
 
  for(int i=0;i<NUMPIXELS;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,255,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
 
  for(int i=0;i<NUMPIXELS;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,0,255)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}

 

Link

Only $1.30 for this board

1pcs 12-bit WS2812 5050 RGB LED intelligent full-color RGB ring development board ring

Share