Analog Joystick controlled RGB LED using Raspberry Pi
Call for Price
Analog Joystick controlled RGB LED using Raspberry Pi
Description
ABSTRACT
Raspberry Pi beginners can start with Analog Joystick interfacing with the Raspberry Pi, to control an RGB LED which has 3 color emission, Each color can be chosen by making movement on Analog Joystick. This application can be reconfigured to control the appliances or Robots with comfortable handling. Analog joystick is differ based on the hardware design, the Joystick which used here returns analog values, so that every value will be linearly changing, which makes you handle application easily. For example This Joystick refers to be returning the value in one channel for either Left or Right. Similarly returns value on other channel for either Up or Down direction.
HARDWARE REQUIRED
- Raspberry Pi
- SD card
- Power supply
- VGA to HDMI converter (Optional)
- MCP3008 (ADC IC)
- Analog Joystick
- RGB LED
SOFTWARE REQUIRED
- Raspbian Stretch OS
- SD card Formatter
- Win32DiskImager (or) Etcher
PYTHON LIBRARIES USED
- RPi.GPIO as GPIO (To access the GPIO Pins of Raspberry Pi)
CODE
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) def readadc(adcnum, clockpin, mosipin, misopin, cspin): if ((adcnum > 7) or (adcnum < 0)): return -1 GPIO.output(cspin, True) GPIO.output(clockpin, False) # start clock low GPIO.output(cspin, False) # bring CS low commandout = adcnum commandout |= 0x18 # start bit + single-ended bit commandout <<= 3 # we only need to send 5 bits here for i in range(5): if (commandout & 0x80): GPIO.output(mosipin, True) else: GPIO.output(mosipin, False) commandout <<= 1 GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout = 0 # read in one empty bit, one null bit and 10 ADC bits for i in range(12): GPIO.output(clockpin, True) GPIO.output(clockpin, False) adcout <<= 1 if (GPIO.input(misopin)): adcout |= 0x1 GPIO.output(cspin, True) adcout >>= 1 # first bit is 'null' so drop it return adcout # change these as desired - they're the pins connected from the # SPI port on the ADC to the Cobbler SPICLK = 18 SPIMISO = 23 SPIMOSI = 24 SPICS = 25 red_led=16 green_led=20 blue_led=21 # set up the SPI interface pins GPIO.setup(SPIMOSI, GPIO.OUT) GPIO.setup(SPIMISO, GPIO.IN) GPIO.setup(SPICLK, GPIO.OUT) GPIO.setup(SPICS, GPIO.OUT) GPIO.setup(red_led, GPIO.OUT) GPIO.setup(green_led, GPIO.OUT) GPIO.setup(blue_led, GPIO.OUT) while True: # read the analog pin digital_channels = [readadc(pin, SPICLK, SPIMOSI, SPIMISO, SPICS) for pin in range(2)] # print (digital_channels) if (0 < digital_channels[0] < 100): print("Left") GPIO.output(red_led, True) else: GPIO.output(red_led, False) if (900 < digital_channels[0] < 1025): print("Right") GPIO.output(green_led, True) else: GPIO.output(green_led, False) if (0 < digital_channels[1] < 100): print("Up") GPIO.output(blue_led, True) else: GPIO.output(blue_led, False) if (900 < digital_channels[1] < 1025): print("down")
Additional information
Weight | 0.000000 kg |
---|
Reviews
There are no reviews yet.