Czujnik temperatury RPi
1.Instalacja interface-u one wire:
1 | sudo raspi-config |
Interfaces options -> 1-wire -> enabled
2.Biblioteka repozytorium
3. Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include "ds18b20.h" #include <bcm2835.h> #include <iostream> using namespace std; int main() { // Initialize bcm2835 library if (!bcm2835_init()) { cerr << "Initialize BCM2835 failed!" << endl; return -1; } // Set GPIO 4 to input with pullup bcm2835_gpio_fsel(4, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(4, BCM2835_GPIO_PUD_UP); DS18B20 ds18b20; while(1){ cout << "Temperature = " << ds18b20.readTemp() << " degrees Celsius" << endl; delay(1000); } return 0; } |
5. Rozbudowa programu o diodę.
Dioda sprawdza, czy przekroczono temperaturę graniczną (26 stopni)
sprzęt dodatkowy:
RPI PIN17->rezystor->dioda noga długa (+) ->dioda(-) -> RPI GND
Kod (autor: Adam Lubojański)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include "ds18b20.h" #include <bcm2835.h> #include <iostream> //pin LED: #define PIN 17 using namespace std; int main() { cout<<"START\n"; // Initialize bcm2835 library if (!bcm2835_init()) { cerr << "Initialize BCM2835 failed!" << endl; return -1; } // Set GPIO 4 to input with pullup bcm2835_gpio_fsel(4, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_set_pud(4, BCM2835_GPIO_PUD_UP); bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP); //LED DS18B20 ds18b20; while(1) { double temp = ds18b20.readTemp(); cout << "Temperature = " << temp << " degrees Celsius" << endl; if(temp>26) { bcm2835_gpio_write(PIN,HIGH); } else { bcm2835_gpio_write(PIN,LOW); } delay(1500); } return 0; } |