Posts: 94
Joined: Fri Aug 10, 2012 8:57 am

DS18b20 in C

Thu Feb 27, 2014 8:35 am

Good Morning, I have started reading the DS18b20 in C and unfortunately my priorities have changed (work commitments) so i thought I would dump the code and it might be useful to someone else.

It reads the temp in degree c and dumps it to terminal it fails when the temp goes below 5 degrees


Code: Select all

#include <stdio.h>
#include <string.h>
#include <fcntl.h>

#define SIZE 1
#define NUMELEM 74

main ()
	FILE *fp = NULL;
	char buff[100];
	char temp_raw[5];
	char temp_c;
	/*Enable Temp*/
	system("sudo modprobe w1-gpio");
	system("sudo modprobe w1-therm");

	fp = fopen("/sys/bus/w1/devices/28-000005c08659/w1_slave","r");

	if (NULL == fp)
	printf("\n fopen Error!!!\n\n");
	return 1;

	if(SIZE*NUMELEM != fread(buff,SIZE,NUMELEM,fp))
	printf("\n fread() failed\n");
	return 1;

	temp_raw[0] = buff[69];
	temp_raw[1] = buff[70];
	temp_raw[2] = buff[71];
	temp_raw[3] = buff[72];
	temp_raw[4] = buff[73];
	temp_raw[5] = buff[74];

	printf("\nTemp Is: %sDegC\n", temp_raw); 

	return 0;
I hope this is useful to someone
I assume I know what I`m talking about... I probably don`t

Home: 256mb Made in UK, Rasbmc, 40" Sony Bravia KDL-40v3000
Work: 2b+, Rasbian, 6" Lilliput touchscreen

Return to “C/C++”