UT 13: I2C
This example shows how to use some I2C functions and how to communicate with a BME280 sensor

Required Materials

1 x Waspmote 1 x MiniUSB wire 1 x Battery

Notes

Use a BME280 sensor to test this example. Open the Waspmote technical guide, page 59, section I/O to see how digital pins and bus pins are located.
This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [Ut_13] Waspmote Using I2C bus Example --------
3
*
4
* Explanation: This example shows how to use some I2C functions and
5
* how to communicate with a BME280 sensor
6
*
7
* Tips: Use a BME280 sensor to test this example
8
* Open the Waspmote technical guide, page 59, section I/O
9
* to see how digital pins and bus pins are located
10
*
11
* Copyright (C) 2018 Libelium Comunicaciones Distribuidas S.L.
12
* http://www.libelium.com
13
*
14
* This program is free software: you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation, either version 3 of the License, or
17
* (at your option) any later version.
18
*
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with this program. If not, see .
26
*
27
* Version: 3.0
28
* Design: David Gascón
29
* Implementation: Luis Miguel Martí
30
*/
31
32
// Variable to store function returns
33
uint8_t data_read;
34
35
36
void setup()
37
{
38
//////////////////////////
39
// 1. Start the I2C bus
40
//////////////////////////
41
I2C.begin();
42
43
////////////////////////////////////////////////////////
44
// Power on the socket
45
////////////////////////////////////////////////////////
46
// Connect device Vcc pin to the 3V3 SENSOR POWER pin
47
// If the device requiers 5V power supply, the 5V SENSOR
48
// POWER pin may be enabled with:
49
// PWR.setSensorPower(SENS_5V, SENS_ON);
50
////////////////////////////////////////////////////////
51
PWR.setSensorPower(SENS_3V3,SENS_ON);
52
}
53
54
void loop()
55
{
56
///////////////////////////////////////////////////////////
57
// 2. Read data from slave device connected to the I2C bus
58
///////////////////////////////////////////////////////////
59
// 0x77 -> slave device address
60
// 0xD0 -> register address to read data from
61
// data_read -> variable to store data read
62
// 1 -> size of data read
63
64
I2C.read(0x77, (uint8_t)0xD0, &data_read, 1);
65
66
USB.print(F("Data read from I2C device:"));
67
USB.println(data_read,HEX);
68
69
delay(1000);
70
71
/////////////////////////////////////////////////////////////
72
// 3. Write data in the address register of the slave device
73
/////////////////////////////////////////////////////////////
74
// 0x77 -> slave device address
75
// 0xD1 -> register address where data is written
76
// 0x01 -> data stored into the register address of the device
77
I2C.write(0x77, 0xD1, 0x01);
78
79
delay(10000);
80
}
Copied!
Last modified 1yr ago