I have an RPI 4 model B
below is my code
this is use for universal coin selector
DC12V -> power supply +
GND -> Power supply GND and PI GND
COIN PIN -> RPI GPIO 24
this works fine atleast but problem occurs when I try to connect USB devices in the RPI I even have a separated USB HUB for the printers but not working. I also have the official powersupply for RPI so not sure where's the error
below is my code
Code:
import RPi.GPIO as GPIOimport time# GPIO pin connected to the COIN pin of the coin selectorCOIN_PIN = 24# Setup GPIOGPIO.setmode(GPIO.BCM)GPIO.setup(COIN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)SET_PIN = 18GPIO.setup(SET_PIN, GPIO.OUT)# Variable to track pulse count and time of the last pulsepulse_count = 0last_pulse_time = time.time() # Initialize last pulse time# Threshold for detecting no more pulses (in seconds)NO_PULSE_THRESHOLD = 1 # 1 second# Minimum time between valid pulses (debounce duration in seconds)DEBOUNCE_TIME = 0.02 # 20 milliseconds# Timestamp of the last valid pulselast_valid_pulse = 0#GPIO.output(SET_PIN, GPIO.HIGH)# Callback function to detect coin insertiondef coin_detected(channel): global pulse_count, last_pulse_time, last_valid_pulse current_time = time.time() GPIO.output(SET_PIN, GPIO.LOW) print("counting") # Debounce logic: Ignore pulses too close to each other if current_time - last_valid_pulse > DEBOUNCE_TIME: pulse_count += 1 last_pulse_time = current_time # Update time of the last pulse last_valid_pulse = current_time# Detect coin insertion (using rising edge detection)GPIO.add_event_detect(COIN_PIN, GPIO.RISING, callback=coin_detected, bouncetime=10)print("Waiting for pulses... Press Ctrl+C to exit.")try: while True: current_time = time.time() # Check if no pulses have been detected for the threshold duration if pulse_count > 0 and current_time - last_pulse_time > NO_PULSE_THRESHOLD: print(f"Final pulse count: {pulse_count}") GPIO.output(SET_PIN, GPIO.HIGH) print("No more pulses detected.") pulse_count = 0 # Reset pulse count for the next cycle time.sleep(0.1) # Small delay to prevent high CPU usage #GPIO.output(SET_PIN, GPIO.HIGH)except KeyboardInterrupt: print("\nExiting...") GPIO.cleanup()
DC12V -> power supply +
GND -> Power supply GND and PI GND
COIN PIN -> RPI GPIO 24
this works fine atleast but problem occurs when I try to connect USB devices in the RPI I even have a separated USB HUB for the printers but not working. I also have the official powersupply for RPI so not sure where's the error
Statistics: Posted by mugichan — Wed Jan 22, 2025 3:27 pm — Replies 0 — Views 32