from machine import Pin
import time
# Daftar GPIO untuk LED dan push button
led_pins = [2, 3, 4, 5, 6, 7, 16] # Output LED
button_pins = [9, 10, 11, 12, 13, 14, 17] # Input dari push button
# Inisialisasi LED sebagai output
leds = [Pin(pin, Pin.OUT) for pin in led_pins]
# Inisialisasi push button sebagai input dengan pull-down
buttons = [Pin(pin, Pin.IN, Pin.PULL_DOWN) for pin in button_pins]
while True:
for i in range(7):
if buttons[i].value() == 1: # Jika push button ditekan
leds[i].on() # Nyalakan LED
else:
leds[i].off() # Matikan LED
time.sleep(0.05) # Delay untuk debounce sederhana
2. Led RGB, Touch Sensor, & Sensor Infrared

#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
uint8_t ir_status = HAL_GPIO_ReadPin(GPIOB, IR_Pin); // Membaca IR sensor
(PB10)
uint8_t touch_status = HAL_GPIO_ReadPin(GPIOB, TOUCH_Pin); // Membaca
Touch Sensor (PB6)
// LED Biru menyala jika IR aktif
HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, ir_status);
// LED Hijau menyala jika Touch aktif
HAL_GPIO_WritePin(GPIOA, GREEN_Pin, touch_status);
// LED Merah menyala jika tidak ada sensor yang aktif
if (ir_status == GPIO_PIN_RESET && touch_status == GPIO_PIN_RESET) {
HAL_GPIO_WritePin(GPIOA, RED_Pin, GPIO_PIN_SET); // Nyalakan LED
RED
} else {
HAL_GPIO_WritePin(GPIOA, RED_Pin, GPIO_PIN_RESET); // Matikan LED
RED
}
HAL_Delay(10); // Delay kecil untuk stabilisasi pembacaan sensor
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType =
RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, RED_Pin|GREEN_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : RED_Pin GREEN_Pin */
GPIO_InitStruct.Pin = RED_Pin|GREEN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : BLUE_Pin */
GPIO_InitStruct.Pin = BLUE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BLUE_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : IR_Pin TOUCH_Pin */
GPIO_InitStruct.Pin = IR_Pin|TOUCH_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif /* USE_FULL_ASSERT */
3. Led & PIR
from machine import Pin
import time
# Konfigurasi sensor PIR sebagai input
pir = Pin(27, Pin.IN)
# Konfigurasi LED sebagai output
led_merah = Pin(15, Pin.OUT) # LED merah menyala saat ada gerakan
led_kuning = Pin(14, Pin.OUT) # LED kuning menyala saat tidak ada gerakan
print("Menunggu gerakan...")
while True:
if pir.value(): # Jika sensor PIR mendeteksi Gerakan
led.value(0) # Matikan LED
print("Gerakan terdeteksi!")
led_merah.value(1) # Nyalakan LED merah
led_kuning.value(0) # Matikan LED kuning
else:
led_merah.value(0) # Matikan LED merah
led_kuning.value(1) # Nyalakan LED kuning
time.sleep(0.1) # Delay untuk menghindari pembacaan cepat
4. Led RGB, PIR, & Touch Sensor
#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
uint8_t pir_status = HAL_GPIO_ReadPin(GPIOB, PIR_Pin);
uint8_t touch_status = HAL_GPIO_ReadPin(GPIOB, TOUCH_Pin);
HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, pir_status);
if (touch_status == GPIO_PIN_SET) {
HAL_GPIO_WritePin(GPIOA, RED_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GREEN_Pin, GPIO_PIN_SET);
HAL_Delay(3000);
HAL_GPIO_WritePin(GPIOA, GREEN_Pin, GPIO_PIN_RESET);
HAL_Delay(3000);
} else {
if (pir_status == GPIO_PIN_RESET && touch_status == GPIO_PIN_RESET) {
HAL_GPIO_WritePin(GPIOA, RED_Pin, GPIO_PIN_SET);
} else {
HAL_GPIO_WritePin(GPIOA, RED_Pin, GPIO_PIN_RESET);
}
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType =
RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, RED_Pin|GREEN_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = RED_Pin|GREEN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BLUE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BLUE_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = PIR_Pin|TOUCH_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
5. Buzzer & Push Button
from machine import Pin
import utime
# Konfigurasi pin
BUZZER = Pin(12, Pin.OUT)
Button1= Pin(10, Pin.IN, Pin.PULL_UP)
Button2= Pin(7, Pin.IN, Pin.PULL_UP)
lastButton1State = True
lastButton2State = True
def buzz(duration):
BUZZER.value(1) # Nyalakan buzzer
utime.sleep_ms(duration)
BUZZER.value(0) # Matikan buzzer
while True:
Button1State = Button1.value()
Button2State = Button2.value()
if Button1State == 0 and lastButton1State == 1:
print("Tombol 1 Ditekan")
buzz(100)
lastButton1State = Button1State
if Button2State == 0 and lastButton2State == 1:
print("Tombol 2 Ditekan")
buzz(100)
lastButton2State = Button2State
utime.sleep_ms(1) # Debounce sederhana
6. Buzzer, LED RGB, Push Button, dan Sensor Infrared

#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
uint8_t button_status = HAL_GPIO_ReadPin(GPIOB, BUTTON_Pin);
uint8_t ir_status = HAL_GPIO_ReadPin(GPIOB, IR_Pin);
HAL_GPIO_WritePin(GPIOA, GREEN_Pin | RED_Pin | BUZZER_Pin,
GPIO_PIN_RESET);
if (button_status == GPIO_PIN_SET)
{
HAL_GPIO_WritePin(GPIOA, RED_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_SET);
}
if (ir_status == GPIO_PIN_SET)
{
HAL_GPIO_WritePin(GPIOA, GREEN_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_SET);
}
HAL_Delay(100);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, RED_Pin|GREEN_Pin|BUZZER_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = RED_Pin|GREEN_Pin|BUZZER_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BLUE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BLUE_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BUTTON_Pin|IR_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
7. Led RGB, Buzzer, & Push Button

from machine import Pin
import utime
# Konfigurasi pin
LED_RED = Pin(5, Pin.OUT)
LED_GREEN = Pin(6, Pin.OUT)
LED_BLUE = Pin(11, Pin.OUT)
BUZZER = Pin(12, Pin.OUT)
BTN_RED = Pin(10, Pin.IN, Pin.PULL_UP)
BTN_GREEN = Pin(7, Pin.IN, Pin.PULL_UP)
BTN_BLUE = Pin(8, Pin.IN, Pin.PULL_UP)
# Variabel status LED (awalannya mati)
redState = False
greenState = False
blueState = False
lastRedBtnState = True
lastGreenBtnState = True
lastBlueBtnState = True
def updateLEDs():
LED_RED.value(redState) # Common Katode
LED_GREEN.value(greenState)
LED_BLUE.value(blueState)
def buzz(duration):
BUZZER.value(1) # Nyalakan buzzer
utime.sleep_ms(duration)
BUZZER.value(0) # Matikan buzzer
while True:
redBtnState = BTN_RED.value()
greenBtnState = BTN_GREEN.value()
blueBtnState = BTN_BLUE.value()
if redBtnState == 0 and lastRedBtnState == 1:
redState = not redState
print("Tombol Merah Ditekan")
buzz(100)
lastRedBtnState = redBtnState
if greenBtnState == 0 and lastGreenBtnState == 1:
greenState = not greenState
print("Tombol Hijau Ditekan")
buzz(100)
lastGreenBtnState = greenBtnState
if blueBtnState == 0 and lastBlueBtnState == 1:
blueState = not blueState
print("Tombol Biru Ditekan")
buzz(100)
lastBlueBtnState = blueBtnState
updateLEDs()
utime.sleep_ms(50) # Debounce sederhana
8. Led RGB, Buzzer, Touch Sensor, & PIR

#include "main.h"
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
uint8_t pir_status = HAL_GPIO_ReadPin(GPIOB, PIR_Pin);
uint8_t touch_status = HAL_GPIO_ReadPin(GPIOB, TOUCH_Pin);
HAL_GPIO_WritePin(GPIOA, GREEN_Pin | RED_Pin | BUZZER_Pin,
GPIO_PIN_RESET);
if (pir_status == GPIO_PIN_SET)
{
HAL_GPIO_WritePin(GPIOA, RED_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_SET);
}
if (touch_status == GPIO_PIN_SET)
{
HAL_GPIO_WritePin(GPIOA, GREEN_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOA, BUZZER_Pin, GPIO_PIN_SET);
}
HAL_Delay(100);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType =
RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOA, RED_Pin|GREEN_Pin|BUZZER_Pin,
GPIO_PIN_RESET);
HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = RED_Pin|GREEN_Pin|BUZZER_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = BLUE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(BLUE_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = PIR_Pin|TOUCH_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
Komentar
Posting Komentar