Menulis Coding LDR dengan TIGA (3) jenis sistem pengawal Mikro
Analog - bermaksud mempunyai nilai dari 0 sehingga nilai maksimum mengikut kapasiti bit pengawalmikro tersebut. Contoh nilai analog pada arduino Uno adalah 0 - 1023.
Komponen yang diperlukan:
1- Jumper atau wayar
2- LDR
3- LED
4- Perintang 200 -500 Ohm untuk LED
5- Perintang 10,000 Ohm (10K) untuk LDR
Arduino Uno
sambungan komponen
coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int LDR = A0; | |
int nilai = 0; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(LED, OUTPUT); | |
} | |
void loop() { | |
nilai = analogRead(LDR); // bahagikan kepada 4 sekiranya mahu nilai maksimum 255 | |
Serial.println(nilai); | |
delay(500); | |
} |
BBC Microbit
sambungan komponen
coding
Raspberry Pico
sambungan komponen
coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, ADC | |
import utime | |
LDR = ADC(26) | |
while True: | |
nilai = LDR.read_u16() | |
print(nilai) | |
utime.sleep(500) |
Menulis Coding LDR dengan LED
Arduino Uno
sambungan komponen
coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int LEDsatu = 7; | |
int LDR = A0; // sambungan pin ke peranti cahaya | |
int val = 0; | |
void setup() { | |
pinMode(LEDsatu, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
val = analogRead(LDR)/4; | |
Serial.println(val); | |
analogWrite(LEDsatu, val); | |
delay(100); | |
} |
BBC Microbit
sambungan komponen
coding
Raspberry Pico
sambungan komponen
coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin, ADC , PWM | |
import utime | |
Ldr = ADC(26) | |
led = PWM(Pin(27)) | |
led.freq(1000) | |
while True: | |
nilai = Ldr.read_u16() | |
led.duty_u16(nilai) | |
print(nilai) | |
utime.sleep(0.5) |
Segmen BONUS:
1- Arduino dan Processing
Coding
sini
2- Microbit dan Peranti LED
Coding
sini
Comments
Post a Comment