Skip to content

Commit

Permalink
Merge pull request #3 from Mireille-T/master
Browse files Browse the repository at this point in the history
Update for micro:bit v2 compatibility
  • Loading branch information
Mireille-T committed Feb 16, 2022
2 parents 3d35e54 + 5ee0a32 commit a5c2539
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Or the driver will complain with a Microbit panic.
This package/library provides:
1. `read_temperature()` - Read ambient tempreture
* reads the temperature from the sensor.
* returns the tempreture as an integer in degree celcius
* returns the tempreture as an integer in degree celsius
2. `read_humidity()` - - Read ambient relative humidity
* reads the relative humidity from the sensor.
* returns the relative humidity in as a integer percentage.
3. `set_i2c_address` - Change the i2c address used to address the sensor.
3. `set_i2c_address` - Change the i2c address used to address the sensor. By default this is set to `0x40`.

## Troubleshooting
1. MicroBit panics _(displays a frowning face) with an error code of 80.
1. MicroBit panics _(displays a frowning face)_ with an error code of 80.
* The driver was unable to read from the sensor. Check whether the sensor
is connected properly.
* If the sensor is connected properly, the driver is still unable to
Expand Down
41 changes: 23 additions & 18 deletions driver_sht2x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,53 @@
* The SHT2x Driver provides access to the functionality of the SHT20, SHT21
* and SHT2x sensors
*/

let sht_i2c_address = 0x40;

//% color=#4c6ef5 weight=25 icon="\uf043" block="SHT2x Sensor"
namespace SHT2xDriver {
/**
* Read Relative Humidity from the SHT2x Sensor.
* Returns a number describing the relative humidity in percentage % relative
* humidity
*/
//% shim=SHT2xDriver::read_humidity
//% blockId="SHT2xDriver_read_humidity"
//% block="read humidty"
export function read_humidity(): number{
// Dummy implementation for the simulator.
console.log("Simulate: Read Humidity from SHT2x sensor: 50");
return 50.0;
export function read_humidity(): number {
pins.i2cWriteNumber(sht_i2c_address, 0xF3, NumberFormat.UInt8LE, false);
basic.pause(100);
let buff = pins.i2cReadBuffer(sht_i2c_address, 3);
let result = buff[0] << 8;
result |= buff[1];
result = (((15625 * result) >> 13) - 6000) / 1000;
return result;
}

/**
* Read Temperature in degrees celcius from the SHT2x sensor.
* Returns a number describing the ambient temperature in degrees celcius
* Returns a number describing the ambient temperature in degrees celsius
*/
//% shim=SHT2xDriver::read_temperature
//% blockId="SHT2xDriver_read_temperature"
//% block="read temperature"
export function read_temperature(): number{
// Dummy implementation for the simulator.
console.log("Simulate: Read Temperature from SHT2x sensor: 50");
return 30.0;
export function read_temperature(): number {
pins.i2cWriteNumber(sht_i2c_address, 0xF3, NumberFormat.UInt8LE, false);
basic.pause(100);
let buff = pins.i2cReadBuffer(sht_i2c_address, 3);
let result = buff[0] << 8;
result |= buff[1];
result = (((21965 * result) >> 13) - 46850) / 1000;
return result;
}

/**
* Change the I2c address used to interact with the SHT2x to the given
* address. The I2c address should be within 0-127.
*/
//% shim=SHT2xDriver::set_i2c_address
//% blockId="SHT2xDriver_set_i2c_address"
//% block="Change i2c address to %address"
//% advanced=true
export function set_i2c_address(address:number)
{
// Dummy implementation for the simulator.
if(address < 0 || address > 128)
{ console.log("Simulate: Invaild i2c Address: " + address); }
else { console.log("Simulate: Set i2c address to " + address); }
export function set_i2c_address(address: number) {
if (address < 0 || address > 128) basic.showString("Invalid I2C address");
else sht_i2c_address = address;
}
}
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 13 additions & 9 deletions pxt.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
{
"name": "sht2x_driver",
"version": "0.0.2",
"description": "Support for the SHT2x Digital Humidity Sensor",
"version": "1.0.0",
"description": "Support for the SHT2x Digital Humidity & Temperature Sensor",
"license": "MIT",
"dependencies": {
"core": "*"
},
"files": [
"README.md",
"driver_sht2x.h",
"driver_sht2x.cpp",
"driver_sht2x.ts",
"shims.d.ts",
"enums.d.ts"
"driver_sht2x.ts"
],
"testFiles": [
"test.ts"
],
"public": false
}
"public": false,
"targetVersions": {
"target": "4.0.17",
"targetId": "microbit"
},
"supportedTargets": [
"microbit"
],
"preferredEditor": "tsprj"
}
8 changes: 5 additions & 3 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
serial.setBaudRate(9600);
SHT2xDriver.set_i2c_address(0x40);
basic.forever(() => {
SHT2xDriver.set_i2c_address(0x80);
basic.showString("%RH:" + SHT2xDriver.read_humidity());
basic.showString("TMP: " + SHT2xDriver.read_temperature());
serial.writeLine("%RH:" + SHT2xDriver.read_humidity());
serial.writeLine("TMP: " + SHT2xDriver.read_temperature());
basic.pause(2000);
})

0 comments on commit a5c2539

Please sign in to comment.