Raspberry Pi control relay realizes lighting function

Today, let’s play with a simple Raspberry Pi peripheral development, through the wiringPi library to control the relay and then control the light on and off.

Raspberry Pi Light

For an introduction to the wiringPI library, please see this blog:
https://www.cnblogs.com/lulipro/p/5992172.html

code show as below:

#include <wiringPi>
#include <stdio.h>
#define SWITCHER 7	// 7为树莓派物理引脚编码和wiringPi编码。在树莓派功能名为GPIO.7

int main()
{
    
    
	int cmd;
	if( wiringPiSetup() == -1){
    
    		//调用wiringPI库需要先wiringPiSetup()初始化
		printf("硬件接口初始化失败\n");
		return -1;
	}
	/* 通用GPIO控制函数 void pinMode (int pin, int mode)
	 * pin:配置的引脚
	 * mode:指定引脚的IO模式
	 * 可取的值:INPUT、OUTPUT、PWM_OUTPUT,GPIO_CLOCK
	 * 作用:配置引脚的IO模式
	 */
	pinMode(SWITCHER, OUTPUT);	
		
	while(1)
	{
    
    
		printf("请输入0或1:0->断开开关;1->导通开关\n");
		scanf("%d",&cmd);
		if(cmd == 1){
    
    
			digitalWrite(SWITCHER,LOW);		// void digitalWrite (int pin, int value)  pin:控制的引脚 value:引脚输出的电平值。可取的值:HIGH,LOW分别代表高低电平 让对一个已近配置为输出模式的 引脚  输出指定的电平信号
		}else if(cmd == 0){
    
    
			digitalWrite(SWITCHER,HIGH);
		}else{
    
    
			printf("输入错误\n");
		}
	}
}

The circuit diagram is as follows:
Insert picture description here

Insert picture description here
Insert picture description here

Raspberry Pi pin diagram:

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/zouchengzhi1021/article/details/113811104