Codes LED - act 101

Version Pi - Langage c

1
/**********************************************************************
2
* Filename    : Blink.c
3
* Description : Basic usage of GPIO. Let led blink.
4
* auther      : www.freenove.com
5
* modification: 2019/12/26
6
**********************************************************************/
7
#include <wiringPi.h>
8
#include <stdio.h>
9
10
#define  ledPin    0	//define the led pin number
11
12
void main(void)
13
{	
14
	printf("Program is starting ... \n");
15
	
16
	wiringPiSetup();	//Initialize wiringPi.
17
	
18
	pinMode(ledPin, OUTPUT);//Set the pin mode
19
	printf("Using pin%d\n",ledPin);	//Output information on terminal
20
	while(1){
21
		digitalWrite(ledPin, HIGH);  //Make GPIO output HIGH level
22
		printf("led turned on >>>\n");		//Output information on terminal
23
		delay(1000);						//Wait for 1 second
24
		digitalWrite(ledPin, LOW);  //Make GPIO output LOW level
25
		printf("led turned off <<<\n");		//Output information on terminal
26
		delay(1000);						//Wait for 1 second
27
	}
28
}
29

Version Pi - Python

1
import RPi.GPIO as GPIO
2
import time
3
4
numPin = 18    # la LED ou le buzzer est reliée à la broche 37 (BOARD) ou 18 (BCM)
5
6
def setup():
7
    #GPIO.setmode(GPIO.BOARD)       # Numérotation physique, sinon GPIO.BCM pour la notation BCM
8
    GPIO.setmode(GPIO.BCM)
9
  	GPIO.setup(numPin, GPIO.OUT)   # La broche est configurée en sortie
10
    GPIO.output(numPin, GPIO.LOW)  # On éteint la LED ou le arrête le buzzer
11
    print ('using pin%d'%numPin)   # On affiche le n° de broche
12
def loop():
13
    while True:
14
        GPIO.output(numPin, GPIO.HIGH)  # led ou buzz on
15
        print ('...led ou buzz on')
16
        time.sleep(1) # on attend 1 seconde
17
        GPIO.output(numPin, GPIO.LOW)  # led ou buzz off
18
        print ('led ou buzz off...')
19
        time.sleep(1)
20
21
def destroy():
22
    GPIO.output(numPin, GPIO.LOW)      # led off
23
    GPIO.cleanup()                     # libérer les ressources
24
25
if __name__ == '__main__':     # Démarrage du programme
26
    setup()
27
    try:
28
        loop()
29
    except KeyboardInterrupt:  # Quand 'Ctrl+C' est activé, la méthode destroy est appelée pour mettre fin au programme
30
        destroy() 

Version Pi - Nodered

1
[
2
    {
3
        "id": "8cc4ba1d342e2e95",
4
        "type": "tab",
5
        "label": "Flow 2",
6
        "disabled": false,
7
        "info": ""
8
    },
9
    {
10
        "id": "4947a37ead4f2446",
11
        "type": "inject",
12
        "z": "8cc4ba1d342e2e95",
13
        "name": "",
14
        "props": [
15
            {
16
                "p": "payload"
17
            },
18
            {
19
                "p": "topic",
20
                "vt": "str"
21
            }
22
        ],
23
        "repeat": "2",
24
        "crontab": "",
25
        "once": true,
26
        "onceDelay": 0.1,
27
        "topic": "",
28
        "payload": "0",
29
        "payloadType": "num",
30
        "x": 210,
31
        "y": 60,
32
        "wires": [
33
            [
34
                "438617b8b5702556"
35
            ]
36
        ]
37
    },
38
    {
39
        "id": "438617b8b5702556",
40
        "type": "rpi-gpio out",
41
        "z": "8cc4ba1d342e2e95",
42
        "name": "LED GPIO17-11",
43
        "pin": "11",
44
        "set": true,
45
        "level": "0",
46
        "freq": "",
47
        "out": "out",
48
        "x": 430,
49
        "y": 80,
50
        "wires": [],
51
        "inputLabels": [
52
            "ON/OFF (1/0)"
53
        ]
54
    },
55
    {
56
        "id": "2bc92277862c8f5c",
57
        "type": "inject",
58
        "z": "8cc4ba1d342e2e95",
59
        "name": "",
60
        "props": [
61
            {
62
                "p": "payload"
63
            },
64
            {
65
                "p": "topic",
66
                "vt": "str"
67
            }
68
        ],
69
        "repeat": "2",
70
        "crontab": "",
71
        "once": true,
72
        "onceDelay": "1,1",
73
        "topic": "",
74
        "payload": "1",
75
        "payloadType": "num",
76
        "x": 210,
77
        "y": 120,
78
        "wires": [
79
            [
80
                "438617b8b5702556"
81
            ]
82
        ]
83
    }
84
]

Version C#

1
using System;
2
using System.Device.Gpio;
3
using System.Threading;
4
5
Console.WriteLine("Blinking LED. Press Ctrl+C to end.");
6
int buzzer_pin = 18;
7
using var controller = new GpioController();
8
controller.OpenPin(buzzer_pin, PinMode.Output);
9
bool ledOn = true;
10
while (true)
11
{
12
    controller.Write(buzzer_pin, ((ledOn) ? PinValue.High : PinValue.Low));
13
    Thread.Sleep(1000);
14
    ledOn = !ledOn;
15
}