RaspberryPi控制继电器

 

继电器使用的GPIO为GPIO6(官方),6(wiringPi),对应针脚22。

RPi版本为第一版。

#include <wiringPi.h>
#include <stdlib.h>
#include "pub.h"

int main()
{

        int rev = GetRPiRev();
        printf("###### | RPi rev=%d\n", rev);

        int pinLED = 5;
        int pinRelay = 6;

        if(wiringPiSetup() == -1)
                exit(1);

        printf("###### | Setup OK!\n");

        pinMode(pinLED, OUTPUT);

        while(1)
        {
                pinMode(pinRelay, OUTPUT);
                digitalWrite(pinRelay, LOW);
                digitalWrite(pinLED, HIGH);
                printf("pin:%d, LOW\n", pinRelay);

                delay(5000);

                pinMode(pinRelay, INPUT);
                digitalWrite(pinRelay, HIGH);
                digitalWrite(pinLED, LOW);
                printf("pin:%d, HIGH\n", pinRelay);

                delay(5000);

        }

        return 0;
}

使用时注意以下两句的含义:

pinMode(pinRelay, OUTPUT);

pinMode(pinRelay, INPUT);

如果在digitalWrite()前没有使用以上函数的话,继电器不能开或者闭。

digitalWrite(pinRelay, LOW);代表开,反之代表闭。

 

附:wiringPi官网公布的GPIO针脚对应关系表

P1: The Main GPIO connector:

wiringPi
Pin
BCM
GPIO
Name Header Name BCM
GPIO
wiringPi
Pin
3.3v 1 | 2 5v
8 R1:0/R2:2 SDA0 3 | 4 5v
9 R1:1/R2:3 SCL0 5 | 6 0v
7 4 GPIO7 7 | 8 TxD 14 15
0v 9 | 10 RxD 15 16
0 17 GPIO0 11 | 12 GPIO1 18 1
2 R1:21/R2:27 GPIO2 13 | 14 0v
3 22 GPIO3 15 | 16 GPIO4 23 4
3.3v 17 | 18 GPIO5 24 5
12 10 MOSI 19 | 20 0v
13 9 MISO 21 | 22 GPIO6 25 6
14 11 SCLK 23 | 24 CE0 8 10
0v 25 | 26 CE1 7 11
wiringPi
Pin
BCM
GPIO
Name Header Name BCM
GPIO
wiringPi
Pin
  • Board Revisions: Please note the differences between board revisions 1 and 2 (R1 and R2 above)

P5: The auxilliary GPIO connector present on Rev. 2 boards only:

wiringPi Pin BCM GPIO Name Header Name BCM GPIO wiringPi Pin
5v 1 | 2 3.3v
17 28 GPIO8 3 | 4 GPIO9 29 18
19 30 GPIO10 5 | 6 GPIO11 31 20
0v 7 | 8 0v
wiringPi
Pin
BCM
GPIO
Name Header Name BCM
GPIO
wiringPi
Pin

 

添加新评论