Advertisement
DamagedDolphin

Clock Instructions

Dec 28th, 2024
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. Guide for Setting Up and Running the Analog Voltmeter Clock
  2.  
  3. This guide provides step-by-step instructions to set up and run the Analog Voltmeter Clock using a Raspberry Pi Zero W, Adafruit PWM/Servo Bonnet, and analog voltmeters for hours, minutes, and seconds.
  4. Required Components
  5.  
  6.     Analog Voltmeter (x3)
  7.     DC 62T2/65C5 3V Class 2.5 Analog Voltmeter
  8.     URL: https://www.aliexpress.us/item/2251832716993909.html
  9.  
  10.     PWM/Servo Controller
  11.     Adafruit 16-Channel PWM/Servo Bonnet for Raspberry Pi
  12.     URL: https://www.adafruit.com/product/3416
  13.  
  14.     Microcontroller
  15.     Raspberry Pi Zero W
  16.     URL: https://www.raspberrypi.com/products/raspberry-pi-zero-w/
  17.  
  18.     Optional: PiSugar Battery Pack
  19.     PiSugar 2 for Raspberry Pi Zero W
  20.     URL: https://www.tindie.com/products/pisugar/pisugar-2-battery-for-raspberry-pi-zero/
  21.  
  22.     Miscellaneous Components
  23.         Micro-USB cable and power adapter.
  24.         Soldering iron (if headers need to be soldered onto the Raspberry Pi or PWM bonnet).
  25.  
  26. Wiring Configuration
  27.  
  28.     Connect the PWM Bonnet to the Raspberry Pi
  29.         Align the 40-pin GPIO header on the Adafruit PWM/Servo Bonnet with the Raspberry Pi's GPIO pins. Push down firmly.
  30.  
  31.    Connect the Voltmeters to the PWM Bonnet
  32.    Assign each voltmeter to a specific PWM channel:
  33.        Seconds Needle (clockSeconds): PWM Channel 2
  34.        Minutes Needle (clockMinutes): PWM Channel 1
  35.        Hours Needle (clockHours): PWM Channel 0
  36.  
  37.    Wiring details for each voltmeter:
  38.        GND Pin: Connect to the GND terminal of the PWM bonnet.
  39.        VCC Pin: Connect to the respective PWM channel terminal.
  40.  
  41.    Power Supply
  42.        Use a micro-USB cable and power adapter to supply power to the Raspberry Pi and PWM Bonnet.
  43.  
  44.    Optional: PiSugar RTC Battery Pack
  45.        Attach the PiSugar 2 battery pack to the Raspberry Pi Zero W to enable portable power and real-time clock functionality.
  46.  
  47. Software Setup
  48. 1. Install Raspberry Pi OS
  49.  
  50.    Flash Raspberry Pi OS (Lite or Desktop) onto an SD card using a tool like Balena Etcher.
  51.    Insert the SD card into the Raspberry Pi and boot it up.
  52.  
  53. 2. Enable I2C on the Raspberry Pi
  54.  
  55.    Open the Raspberry Pi configuration tool:
  56.  
  57. sudo raspi-config
  58.  
  59. Navigate to Interface Options > I2C and enable it.
  60. Reboot the Raspberry Pi:
  61.  
  62.    sudo reboot
  63.  
  64. 3. Install Required Python Libraries
  65.  
  66.    Update and install Python 3 and pip:
  67.  
  68. sudo apt update && sudo apt install python3-pip -y
  69.  
  70. Install the Adafruit CircuitPython libraries:
  71.  
  72.    sudo pip3 install adafruit-circuitpython-pca9685
  73.  
  74. Running the Clock Software
  75.  
  76.    Download the Clock Code
  77.    Save the provided Python code as clock.py in the directory /home/pi/.
  78.  
  79.    Calibrate the Voltmeters
  80.        Before running the clock, calibrate the voltmeters to ensure accurate positioning of the needles.
  81.        Run the calibration mode:
  82.  
  83.    python3 clock.py --calibrate
  84.  
  85.    Follow the on-screen instructions to fine-tune the PWM values for each step (hours, minutes, seconds). Calibration data will be saved to calibration_data.json.
  86.  
  87. Run the Clock
  88.  
  89.    Once calibrated, start the clock:
  90.  
  91.        python3 clock.py
  92.  
  93. How the Code Works
  94. Key Components
  95.  
  96.    PWM Channels for Voltmeters:
  97.        clockSeconds: Controls the seconds needle (PWM Channel 2).
  98.        clockMinutes: Controls the minutes needle (PWM Channel 1).
  99.        clockHours: Controls the hours needle (PWM Channel 0).
  100.  
  101.    Calibration (--calibrate):
  102.        The program adjusts the PWM values for specific positions of the voltmeter needles (e.g., 0, 10, 20, etc.).
  103.        Calibration data is saved in calibration_data.json.
  104.  
  105.    Time Interpolation:
  106.        The interpolate_pwm function calculates intermediate PWM values for fractional times (e.g., 10:30:45).
  107.  
  108.    Smooth Transitions:
  109.        The move_needle_smoothly function ensures the voltmeter needles move smoothly between positions.
  110.  
  111. Code Structure
  112.  
  113. # Initialization:
  114. i2c = busio.I2C(board.SCL, board.SDA)
  115. hat = adafruit_pca9685.PCA9685(i2c)
  116. hat.frequency = 60
  117.  
  118. # Assign channels:
  119. clockSeconds = hat.channels[2]
  120. clockMinutes = hat.channels[1]
  121. clockHours = hat.channels[0]
  122.  
  123. # Calibration:
  124. python3 clock.py --calibrate
  125.  
  126. # Clock Execution:
  127. python3 clock.py
  128.  
  129. Adding the Clock to Startup
  130.  
  131. To run the clock automatically on boot:
  132.  
  133.    Create a Systemd Service
  134.  
  135. sudo nano /etc/systemd/system/analog-clock.service
  136.  
  137. Service Configuration Add the following to the file:
  138.  
  139. [Unit]
  140. Description=Analog Voltmeter Clock
  141. After=multi-user.target
  142.  
  143. [Service]
  144. Type=simple
  145. ExecStart=/usr/bin/python3 /home/pi/clock.py
  146. Restart=on-failure
  147.  
  148. [Install]
  149. WantedBy=multi-user.target
  150.  
  151. Enable the Service
  152.  
  153. sudo systemctl daemon-reload
  154. sudo systemctl enable analog-clock.service
  155. sudo systemctl start analog-clock.service
  156.  
  157. Check Service Status
  158.  
  159.    sudo systemctl status analog-clock.service
  160.  
  161. Troubleshooting
  162.  
  163.    No Calibration File Found: Run python3 clock.py --calibrate to create one.
  164.    PWM Not Working: Verify the I2C connection with:
  165.  
  166.    sudo i2cdetect -y 1
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement