Giter Club home page Giter Club logo

pico-onewire's Introduction

Raspberry Pi Pico One Wire Library

This library allows you to talk to one wire devices such as Temperature sensors Dallas DS18S20, DS18B20 and DS1822, Maxim MAX31820 and MAX31826.

It should also be easy to extend it to also support RTC's DS2404 and DS2417, Current measurement devices DS2740 and 1k EEPROM's DS2502.

Based upon Erik Olieman's mbed DS1820 lib

https://developer.mbed.org/components/DS1820/

Modified timings so that it works when used on the Pi Pico

Added off device test code with mocks and its own cmake configuration.

Modified API's for two stage workflow:

  1. Listing of devices with addresses to be stored elsewhere
  2. Returning temperatures for each device by address

Usage - Temperature reading

Include library

Check the module out as a submodule of your project to say: modules/pico-onewire then add the sub directory and link the library in your CMakeLists.txt:

add_subdirectory(modules/pico-onewire)

target_link_libraries( ... pico_one_wire)

Single sensor

An example main that will check for attached devices once per second, print the address and temperature.

#include <cstdio>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "modules/pico-onewire/api/one_wire.h"

int main() {
    stdio_init_all();
    One_wire one_wire(15); //GP15 - Pin 20 on Pi Pico
    one_wire.init();
    rom_address_t address{};
    while (true) {
        one_wire.single_device_read_rom(address);
        printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x\n", address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], address.rom[5], address.rom[6], address.rom[7]);
        one_wire.convert_temperature(address, true, false);
        printf("Temperature: %3.1foC\n", one_wire.temperature(address));
        sleep_ms(1000);
    }
    return 0;
}

Multiple sensors

A more complicated example to print all the available devices. Additionally pin 22 is used to exit the main loop and return to the USB Flashing mode. A push button switch between pins 28&29 (GND+GP22) will do nicely.

Add to the CMakeLists.txt in your project config:

add_definitions(-DPICO_ENTER_USB_BOOT_ON_EXIT)

main.cpp:

#include <cstdio>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "modules/pico-onewire/api/one_wire.h"

#define TEMP_SENSE_GPIO_PIN 15
#define EXIT_GPIO_PIN 22

int main() {
	stdio_init_all();
	One_wire one_wire(TEMP_SENSE_GPIO_PIN);
	one_wire.init();
	gpio_init(EXIT_GPIO_PIN);
	gpio_set_dir(EXIT_GPIO_PIN, GPIO_IN);
	gpio_pull_up(EXIT_GPIO_PIN);
	sleep_ms(1);
	while (gpio_get(EXIT_GPIO_PIN)) {
		int count = one_wire.find_and_count_devices_on_bus();
		rom_address_t null_address{};
		one_wire.convert_temperature(null_address, true, true);
		for (int i = 0; i < count; i++) {
			auto address = One_wire::get_address(i);
			printf("Address: %02x%02x%02x%02x%02x%02x%02x%02x\r\n", address.rom[0], address.rom[1], address.rom[2],
				   address.rom[3], address.rom[4], address.rom[5], address.rom[6], address.rom[7]);
			printf("Temperature: %3.1foC\n", one_wire.temperature(address));
		}
		sleep_ms(1000);
	}
	return 0;
}

Running the test code on a desktop

If your just using the library you don't need to worry about the test code. If you plan on fixing bugs or extending the library then you must.

Install Catch2 version 3, it might be available on your desktop linux distro, though possibly only an older version. Or you can download, build and install it:

git clone https://github.com/catchorg/Catch2.git
cd Catch2
cmake -Bbuild -H. -DBUILD_TESTING=OFF
cmake --build build/
sudo cmake --build build/ --target install

Build and run the test code:

cd test
mkdir build
cd build
cmake ..
make
./tests

You should then see 'All tests passed'

pico-onewire's People

Contributors

adamboardman avatar polarbearing avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.