client
pigpio
EXAMPLES
C
HALL_EFFECT_SENSOR
hall.c
Go to the documentation of this file.
1
#include <stdio.h>
2
3
#include <
pigpio.h
>
4
5
/*
6
OH3144E or equivalent Hall effect sensor
7
8
Pin 1 - 5V
9
Pin 2 - Ground
10
Pin 3 - gpio (here P1-8, gpio 14, TXD is used)
11
12
The internal gpio pull-up is enabled so that the sensor
13
normally reads high. It reads low when a magnet is close.
14
15
gcc -o hall hall.c -lpigpio -lrt -lpthread
16
sudo ./hall
17
*/
18
19
#define HALL 14
20
21
void
alert
(
int
gpio,
int
level, uint32_t tick)
22
{
23
static
uint32_t lastTick=0;
24
25
if
(lastTick) printf(
"%d %.2f\n"
, level, (
float
)(tick-lastTick)/1000000.0);
26
else
printf(
"%d 0.00\n"
, level);
27
28
lastTick = tick;
29
}
30
31
int
main
(
int
argc,
char
*argv[])
32
{
33
int
secs=60;
34
35
if
(argc>1) secs = atoi(
argv
[1]);
/* program run seconds */
36
37
if
((secs<1) || (secs>3600)) secs = 3600;
38
39
if
(
gpioInitialise
()<0)
return
1;
40
41
gpioSetMode
(
HALL
,
PI_INPUT
);
42
43
gpioSetPullUpDown
(
HALL
,
PI_PUD_UP
);
44
45
gpioSetAlertFunc
(
HALL
,
alert
);
46
47
sleep(secs);
48
49
gpioTerminate
();
50
}
51
PI_INPUT
#define PI_INPUT
Definition:
pigpio.h:581
gpioSetPullUpDown
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
Definition:
pigpio.c:8661
gpioTerminate
void gpioTerminate(void)
Definition:
pigpio.c:8495
gpioInitialise
int gpioInitialise(void)
Definition:
pigpio.c:8459
gpioSetAlertFunc
int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
Definition:
pigpio.c:11303
argv
argv
gpioSetMode
int gpioSetMode(unsigned gpio, unsigned mode)
Definition:
pigpio.c:8607
pigpio.h
HALL
#define HALL
Definition:
hall.c:19
PI_PUD_UP
#define PI_PUD_UP
Definition:
pigpio.h:594
alert
void alert(int gpio, int level, uint32_t tick)
Definition:
hall.c:21
main
int main(int argc, char *argv[])
Definition:
hall.c:31
cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Fri Aug 2 2024 09:40:56