Categories

KY038 Microphone Module and Arduino example

The ky-38 sound sensor is a very basic sound level detector module which features an electric condenser microphone. It is part of  a sensor kit that can be purchased and the main part of the module is an LM393 comparator. There are 2 LEDs on board, one is for power and the other indicates when the sensor reaches a certain value

The board has 2 outputs

  1. AO, analog output, real-time output voltage signal of the microphone
  2. DO, when the sound intensity reaches a certain value, the output goes high

The sensitivity can be adjusted via the potentiometer on the module

Here is a schematic of the module

Connection

This is the basic connection of the Ky-038 module to an Arduino Uno

 

Basic Example

int soundPin = A0;
int sensorValue = 0;
 
void setup () 
{
Serial.begin (9600);
}
 
void loop () 
{
sensorValue = analogRead (soundPin);
Serial.println (sensorValue, DEC);
delay (1000);
}

Output

Open the serial monitor and make noise near the microphone, you should values – the higher the value the louder the noise

165
28
27
29
27
16
228

Another Example

This example will switch on the on board Arduino LED if the analog value exceeds 200.

int soundPin = A0;
int ledPin = 13;
int sensorValue = 0;
 
void setup () 
{
  Serial.begin (9600);
  pinMode (ledPin, OUTPUT);
}
 
void loop () 
{
  sensorValue = analogRead (soundPin);
  Serial.println (sensorValue, DEC);
  //if sensor goes above max light led (could be buzzer)
  if(sensorValue > 200)
  {
    digitalWrite (ledPin, HIGH);
    delay (1000);
  }
  //switch off LED
  digitalWrite (ledPin, LOW);
  delay (1000);
}

 

Links

A very low cost module, comes in under $0.50

KY-038 4pin Mini Voice Sound Detection Sensor Module Microphone Transmitter

The set of sensors cost about $10

37 IN 1 sensor kit for Arduino starter kit high-quality (Works with Arduino Boards) landzo

Share

A look at Cayenne for Arduino IOT projects

In this post we take a look at cayenne – https://cayenne.mydevices.com/cayenne/signup

This is the blurb from the website

Easy IoT with myDevices Cayenne

Features Include:

  • Remote access your Pi from your mobile phone or computer
  • Remote commands (reboot, shutdown, remote access)
  • Add sensors, actuators, and extensions and create a custom dashboard with drag and drop widgets
  • Monitor data and remote control your devices and projects
  • Remote control sensors/actuators with the dashboard GPIO
  • Easily set up triggers and actions for your automation projects

I like this site as there are many clear examples for Arduino boards and various ways of connecting, my setup was a an Arduino Uno with an Ethernet Shield fitted to it. I decided to add a BMP180 sensor to the ethernet shield as a test example – there is actually an example for this in the github repo – https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino

Connection

ethernet and bmp180

ethernet and bmp180

Code

The default code for the bmp180 is shown, you need to fill in the appropriate details in the authentication section.

This example needs 3 libraries. The Cayenne library, Adafruit BMP085 Unified and Adafruit Sensor library. You can find all of these in the library manager

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <CayenneMQTTEthernet.h>
 
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";
 
#define TEMPERATURE_VIRTUAL_CHANNEL 1
#define BAROMETER_VIRTUAL_CHANNEL 2
 
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
bool bmpSensorDetected = true;
 
void setup()
{
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
	if (!bmp.begin())
	{
		CAYENNE_LOG("No BMP sensor detected");
		bmpSensorDetected = false;
	}
}
 
void loop()
{
	Cayenne.loop();
}
 
// This function is called at intervals to send temperature sensor data to Cayenne.
CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL)
{
	if (bmpSensorDetected)
	{
		float temperature;
		bmp.getTemperature(&temperature);
		// Send the value to Cayenne in Celsius.
		Cayenne.celsiusWrite(TEMPERATURE_VIRTUAL_CHANNEL, temperature);
	}
	else
	{
		CAYENNE_LOG("No BMP sensor detected");
	}
}
 
// This function is called at intervals to send barometer sensor data to Cayenne.
CAYENNE_OUT(BAROMETER_VIRTUAL_CHANNEL)
{
	if (bmpSensorDetected)
	{
		// Send the command to get data.
		sensors_event_t event;
		bmp.getEvent(&event);
 
		if (event.pressure)
		{
			// Send the value to Cayenne in hectopascals.
			Cayenne.hectoPascalWrite(BAROMETER_VIRTUAL_CHANNEL, event.pressure);
		}
	}
	else
	{
		CAYENNE_LOG("No BMP sensor detected");
	}
}

 

Output

Once connected your dashboard should show some data, I renamed them.

 

You can see a chart symbol, clicking on that will display a pop up chart of data, here is what my data looked like. You can also download chart data in csv format and as you can see you can show various date filters of data

Share

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