Raspberry Pi Development-Relay

1. Relay interface:

The relay drive voltage is 3.3v
Insert picture description here

2. Raspberry Pi interface:

gpio readall   // 输入指令查看树莓派 io 口

 +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+

3. Wiring:

The positive pole of the relay is connected to the Raspberry Pi 3.3v, the negative pole of the relay is connected to the Raspberry Pi 0v, and the GPIO port of the relay is connected to the GPIO port
of the Raspberry Pi .
Insert picture description here

4. Code implementation:

#include <stdio.h>
#include <wiringPi.h>

#define GPIO7  7

int main()
{
    
    
        int cmd;
        if(wiringPiSetup() == -1){
    
       // 硬件初始化
                printf("硬件初始化失败!\n");
                return -1;
        }
        pinMode(GPIO7, OUTPUT);   // 配置引脚的 IO 模式,此处我们连接了树莓派 GPIO.7,配置 7 号引脚为输出模式

        while(1){
    
    
        
                printf("请输入0或1,0--断开,1--闭合:\n");
                scanf("%d",&cmd);

                if(cmd == 0){
    
    
                        digitalWrite(GPIO7, HIGH);   // 引脚 GPIO.7 输出高电平
                        printf("开关断开!\n");
                }else if(cmd == 1){
    
    
                        digitalWrite(GPIO7, LOW);   // 引脚 GPIO.7 输出低电平
                        printf("开关闭合!\n");
                }else{
    
    
                        printf("输入错误,");
                }
        }

        return 0;
}

5. Multiple relays:

Use 5v driving voltage:
Insert picture description here
when the light is on, it is in the closed state, and when it is off, it is in the off state

Insert picture description here

#include <stdio.h>
#include <wiringPi.h>

#define GPIO1  1
#define GPIO2  2
#define GPIO3  3
#define GPIO4  4

int main()
{
    
    
	int no,cmd;
	if(wiringPiSetup() == -1){
    
       // 硬件初始化
		printf("硬件初始化失败!\n");
		return -1;
	}
	pinMode(GPIO1, OUTPUT);   // 配置引脚的 IO 模式,配置 1 号引脚为输出模式
	pinMode(GPIO2, OUTPUT);   // 配置引脚的 IO 模式,配置 2 号引脚为输出模式
	pinMode(GPIO3, OUTPUT);   // 配置引脚的 IO 模式,配置 3 号引脚为输出模式
	pinMode(GPIO4, OUTPUT);   // 配置引脚的 IO 模式,配置 4 号引脚为输出模式

	while(1){
    
    

		while(1){
    
    
			printf("请输入需要控制的继电器的序号(1~4):\n");
			scanf("%d",&no);
			if(no==1 || no==2 || no==3 || no==4){
    
    
				break;
			}else{
    
    
				printf("输入错误!\n");
			}
		}

		while(1){
    
    
			printf("%d号继电器——请输入0或1,0--断开,1--闭合:\n",no);
			scanf("%d",&cmd);
			if(cmd==0 || cmd==1){
    
    
				break;
			}else{
    
    
				printf("输入错误!\n");
			}
		}

		switch(no){
    
    

			case 1:
				if(cmd == 0){
    
    
					digitalWrite(GPIO1, HIGH);   // 引脚 GPIO.1 输出高电平
					printf("%d号继电器——开关断开!\n",no);
				}else if(cmd == 1){
    
    
					digitalWrite(GPIO1, LOW);   // 引脚 GPIO.1 输出低电平
					printf("%d号继电器——开关闭合!\n",no);
				}
				break;
				
			case 2:
				if(cmd == 0){
    
    
					digitalWrite(GPIO2, HIGH);   // 引脚 GPIO.2 输出高电平
					printf("%d号继电器——开关断开!\n",no);
				}else if(cmd == 1){
    
    
					digitalWrite(GPIO2, LOW);   // 引脚 GPIO.2 输出低电平
					printf("%d号继电器——开关闭合!\n",no);
				}
				break;
				
			case 3:
				if(cmd == 0){
    
    
					digitalWrite(GPIO3, HIGH);   // 引脚 GPIO.3 输出高电平
					printf("%d号继电器——开关断开!\n",no);
				}else if(cmd == 1){
    
    
					digitalWrite(GPIO3, LOW);   // 引脚 GPIO.3 输出低电平
					printf("%d号继电器——开关闭合!\n",no);
				}
				break;
				
			case 4:
				if(cmd == 0){
    
    
					digitalWrite(GPIO4, HIGH);   // 引脚 GPIO.4 输出高电平
					printf("%d号继电器——开关断开!\n",no);
				}else if(cmd == 1){
    
    
					digitalWrite(GPIO4, LOW);   // 引脚 GPIO.4 输出低电平
					printf("%d号继电器——开关闭合!\n",no);
				}
				break;
		}
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/lcx1837/article/details/108123185