what's button
Buttons are a common component used to control electronic devices. They
are usually used as switches to connect or disconnect circuits. Although
buttons come in a variety of sizes and shapes, the one used in this
experiment will be a 12mm button as shown in the following pictures. Pins
pointed out by the arrows of same color are meant to be connected.
The button we used is a normally open type button. The two contacts of a
button is in the off state under the normal conditions, only when the button
is pressed they are closed
S | Digital keys output |
+ | 3.3v |
- | GND |
1 * button
1 * microbit
- Several Jumper wires
connection diagram:
Code:
makecode:
MU python:
#----------------------------------------------------------- # File name : Touch button.py # Description : The key state is displayed in led matrix lamp. # Author : jason # E-mail : jason@adeept.com # Website : www.adeept.com # Date : 2019/01/15 #----------------------------------------------------------- from microbit import * while True: buttonState = pin5.read_digital() if buttonState==1: display.show(Image.YES) else: display.clear() sleep(20)
Click “flash” and download the code onto the micro:bit.
Effect Picture:
Components
- 1 * Adeept Arduino UNO R3 Board
- 1 * Button Module
- 1 * LED Module
- 1 * USB Cable
- 2 * 3-Pin Wires
- 2 * Hookup Wire Set
- 1 * Breadboard
Step 1: Build the circuit
Adeept UNO R3 Board | Button Module | LED Module |
GND | - | - |
5V | + | |
D2 | S | |
D11 | + |
Code:
/*********************************************************** File name: _02_ButtonModule.ino Description: When you press the button, you can see the state of the LED will be toggled. (ON->OFF,OFF->ON). Website: www.adeept.com E-mail: support@adeept.com Author: Tom Date: 2019/01/15 ***********************************************************/ int ledpin=13; //definition digital 11 pins as pin to control the LED module int btnpin=2; //Set the digital 2 to button module interface volatile int state = LOW; // Defined output status LED Interface void setup() { pinMode(ledpin,OUTPUT);//Set digital 11 port mode, the OUTPUT for the output pinMode(btnpin,INPUT); //Set digital 2 port mode, the INPUT for the input } void loop() { if(digitalRead(btnpin)==HIGH) //Detection button interface to low { delay(10); //Delay 10ms for the elimination of key leading-edge jitter if(digitalRead(btnpin)==HIGH) //Confirm button is pressed { while(digitalRead(btnpin)==HIGH);//Wait for key interfaces to high delay(10); //delay 10ms for the elimination of key trailing-edge jitter while(digitalRead(btnpin)==HIGH);//Confirm button release state = !state; //Negate operation, each time you run the program here, state the HGIH becomes LOW, or the state becomes the LOW HGIH. digitalWrite(ledpin,state); //Output control status LED, ON or OFF } } }
Compile and download the sketch to the UNO R3 board.
Press the button and you can see the LED toggle between on and off.
Components
- 1 * Raspberry Pi
- 1 * GPIO Extension Board
- 1 * 40-Pin GPIO Cable
- 1 * Breadboard
- 1 * Button Module
- 1 * LED Module
- 2 * 3-Pin Wires
Build the circuit
Code:
C code:
code 1
/* * File name : btnAndLed_1.c * Description : Toggle a led by button. * Website : www.adeept.com * E-mail : support@adeept.com * Author : Jason * Date : 2019/01/15 */ #include <wiringPi.h> #include <stdio.h> #define LedPin 0 #define ButtonPin 1 int status = 1; void myISR(void) { status = !status; } int main(void) { if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen printf("setup wiringPi failed !\n"); return -1; } pinMode(LedPin, OUTPUT); pinMode(ButtonPin, INPUT); pullUpDnControl(ButtonPin, PUD_UP); if(wiringPiISR(ButtonPin, INT_EDGE_FALLING, myISR) < 0){ printf("ISR setup error!\n"); return -1; } while(1){ digitalWrite(LedPin, status); } return 0; }
code 2
/* * File name : btnAndLed_2.c * Description : Toggle a led by button. * Website : www.adeept.com * E-mail : support@adeept.com * Author : Jason * Date : 2019/01/15 */ #include <wiringPi.h> #include <stdio.h> #define LedPin 0 #define ButtonPin 1 int status = 1; int main(void) { if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen printf("setup wiringPi failed !\n"); return -1; } pinMode(LedPin, OUTPUT); pinMode(ButtonPin, INPUT); pullUpDnControl(ButtonPin, PUD_UP); while(1){ if(digitalRead(ButtonPin) == 0){ delay(10); if(digitalRead(ButtonPin) == 0){ status = !status; digitalWrite(LedPin, status); //toggle the status of led printf("The status of led is toggled !\n"); } while(!digitalRead(ButtonPin)); } } return 0; }
python code:
code 1
#!/usr/bin/env python import RPi.GPIO as GPIO LedPin = 11 # pin11 --- led BtnPin = 12 # pin12 --- button def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode is input, and pull up to high level(3.3V) GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to make led off def loop(): while True: if GPIO.input(BtnPin) == GPIO.LOW: # Check whether the button is pressed or not. print '...led on' GPIO.output(LedPin, GPIO.LOW) # led on else: print 'led off...' GPIO.output(LedPin, GPIO.HIGH) # led off def destroy(): GPIO.output(LedPin, GPIO.HIGH) # led off GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy()
code 2
#!/usr/bin/env python import RPi.GPIO as GPIO LedPin = 11 # pin11 --- led BtnPin = 12 # pin12 --- button Led_status = 1 def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set BtnPin's mode is input, and pull up to high level(3.3V) GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to make led off def swLed(ev=None): global Led_status Led_status = not Led_status GPIO.output(LedPin, Led_status) # switch led status(on-->off; off-->on) if Led_status == 1: print 'led off...' else: print '...led on' def loop(): GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=swLed) # wait for falling while True: pass # Don't do anything def destroy(): GPIO.output(LedPin, GPIO.HIGH) # led off GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy()
For C language users:
Step 2: Edit and save the code with vim or nano.
(code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/02_btnAndLed/btnAndLed_1.c)
Step 3: Compile
$ sudo gcc btnAndLed_1.c -o btnAndLed -lwiringPi
Step 4: Run
$ sudo ./btnAndLed
For Python users:
Step 2: Edit and save the code with vim or nano.
(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/02_btnAndLed/btnAndLed_1.py)
Step 3: Run
$ sudo python btnAndLed_1.py
Press the button and you can see the LED toggle between on and off
Link for code download:http://www.adeept.com/learn/ Download the kit information as needed.
Video link:http://www.adeept.com/video/