Close

You Can Ring My Bell

A project log for The Phone Friend

Bring any old phone to life, no disassembly required!

stephSteph 04/13/2023 at 18:160 Comments

Ding-a-Ling-a-Ling 

Here's the code to make the bells ring. First we'll be setting up the F/R and RM pins for the AG1171. Then we'll set up a timer so our bells will ring and pause in the correct cadence

The only new library in this code is pwmio, which we'll use to generate the 20hz pulse we need. 

This code will ring the phone whenever it's on hook, and stop the ring signal when the phone phone is picked up.

import digitalio
import board
import time

# We'll use the pwmio library to send an alternating signal 
# to the F/R pin
import pwmio
                    
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT

SHK = digitalio.DigitalInOut(board.GP21)
SHK.direction = digitalio.Direction.INPUT

# Setup pin for ring mode
ringingMode = digitalio.DigitalInOut(board.GP20)
ringingMode.direction = digitalio.Direction.OUTPUT
ringingMode.value = False

# Setup our PWM pin and set 20hz rate (north american ring)
forwardReverse = pwmio.PWMOut(board.GP19, frequency=20, duty_cycle=0x0000)

slicEnable = digitalio.DigitalInOut(board.GP18)
slicEnable.direction = digitalio.Direction.OUTPUT 
slicEnable.value = True
# Per datasheet, AG1171 needs 50ms to wake up
time.sleep(.05) 

def ringBell():
    print('Ringing...')
    ringingMode.value = True
    elapsedTime = 0
    startTime = time.monotonic()

    while not SHK.value: # ring as long as the phone is on the hook
        # ring bells for 2 seconds
        if elapsedTime < 2:        
            forwardReverse.duty_cycle = 0x7FFF #this means 50% duty cycle
            led.value = True
        # bells off for 4 seconds
        elif elapsedTime > 2:
            forwardReverse.duty_cycle = 0x0000 #off
            led.value = False
        # reset elapsed time after 2 + 4 seconds
        if elapsedTime >= 6:
            startTime = time.monotonic()
        
        elapsedTime = time.monotonic() - startTime #update the elapsed time once per loop
        time.sleep(.1)

    # The handset has now been lifted, so stop ringing
    print('...pickup.')
    forwardReverse.duty_cycle = 0x000
    ringingMode.value = False
    
    # Blink led 25 times to indicate pickup/hangup
    for x in range(25):
        led.value = not led.value
        time.sleep(.05)
    led.value = False

# Main loop
while True:
    if not SHK.value:
        ringBell()

Discussions