---
description: Python scripts to control the Survey3 using a Raspberry Pi computer
---

# PWM Control (from RasPi)

Using our [HDMI PWM Trigger cable](https://www.mapir.camera/collections/survey-camera-accessories/products/survey-camera-hdmi-trigger-cable) you can send a PWM command to the Survey3 cameras to perform a couple of actions.

### Raspberry Pi Setup

Connect the HDMI cable to the camera and the single servo connectors to the appropriate GPIO pins on the RasPi GPIO header, as shown below:

![PIN MAP FOR GPIO HEADER ON RASPI](https://roboticsbackend.com/wp-content/uploads/2019/05/raspberry-pi-3-pinout.jpg)

For our below code examples we connected the white signal connector to pin 12 (GPIO18) and the black ground connector to pin 14.

We also connect the included Survey3 USB cable to one of the RasPi USB ports. If you do not need to Mount/Unmount the SD card or power the cameras using USB then you can leave it disconnected.

{% hint style="warning" %}
If the RasPi fails to boot and the USB cable is connected there may be too much current draw required to charge the internal camera battery. We suggest removing the camera battery and then when the USB cable is powered on boot it will automatically turn the camera on.
{% endhint %}

The required PWM signal needs to always send the "do nothing" pulse width. We then change the pulse width only when we want to perform a particular command. It just requires one pulse with the longer width to perform each command.

**PWM Commands:**

* 1000us: Do nothing
* 1500us: Mount/UnMount uSD memory card (inside Survey3 camera)
* 2000us: Trigger photo capture

The 1500us command is sent once to unmount (in software) the uSD memory card inside the camera then mount to the connected USB computer (RasPi), and then 1500us again to mount the uSD back to the camera. This is commonly used to transfer captured photos to the RasPi or to perform post processing on the images.

{% hint style="info" %}
While the uSD is unmounted from the camera it cannot save newly captured images. To view images while mounted you need to use the [WIFI connection and corresponding url commands](https://cdn.shopify.com/s/files/1/0878/4732/files/Survey3-WIFI-Manual.pdf?6626944799327037623?12365009533491002433).
{% endhint %}

### PWM Code:

To send the PWM signals you need to send the 1000us "do nothing" pulse width all the time and then when you want to perform a command you send a momentary 1500us or 2000us width pulse.

```
pin = 18  #set (BCM) GPIO pin to send GPIO.HIGH pulse

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
time.sleep(0.001) #1000us pulse width (do nothing)
GPIO.cleanup()

time.sleep(0.1)

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.002) #2000us pulse width
GPIO.cleanup()
    
time.sleep(0.1)
    
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
time.sleep(0.001) #1000us pulse width (do nothing)
GPIO.cleanup()
```

The above code will set the PWM to the do nothing 1000us, then send a 2000us trigger command to capture a photo, then go back to sending the do nothing 1000us. The quickest the 2000us command should be sent is about once every 1.5s as the camera cannot capture JPG images more quickly than 1.5s. For RAW+JPG mode we recommend a 2.5-3.0s wait time.

The code can be set up to loop until the user exits (CTRL+Z), triggering the capture command every 1.5s:

```
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

pin = 18  #set (BCM) GPIO pin to send GPIO.HIGH pulse

try:
    while True:
    
        print("Command Sent\n")        
    
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.HIGH)
        time.sleep(0.002) #2000us pulse width
        GPIO.cleanup()
    
        time.sleep(0.1)
    
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.OUT)
        time.sleep(0.001) #1000us pulse width (do nothing)
        GPIO.cleanup()
    
        time.sleep(1.5) #time delay between GPIO.HIGH pulses, >1.5s recommended

#to exit type CTRL+Z
except KeyboardInterrupt:
    pass
```

{% file src="https://2255605865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFgqAozKbevkCPwL6f4%2Fuploads%2FsPMQsXnoJcOyS4PcPaia%2Fpwm.py?alt=media&token=3a052034-0cc7-4fe8-9e99-581b86313f71" %}

You could also set it up to take user keyboard inputs:

```
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)

pin = 18  #set (BCM) GPIO pin to send GPIO.HIGH pulse

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
time.sleep(0.001)
GPIO.cleanup()

print("\nCommands:")
print("t + Enter: Trigger (2000us)")
print("s + Enter: SD Card Mount/Unmount (1500us)")
print("e + Enter: Exit\n")

while True:
    key = input(">")
    if key == "t":
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.HIGH)
        time.sleep(0.002)
        GPIO.cleanup()
        
        time.sleep(0.1)
        print("Trigger")        
        
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.OUT)
        time.sleep(0.001)
        GPIO.cleanup()

    if key == "s":
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.HIGH)
        time.sleep(0.0015)
        GPIO.cleanup()
        
        time.sleep(0.1)
        print("SD Mount/UnMount")
        
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.OUT)
        time.sleep(0.001)
        GPIO.cleanup()

    if key == "e":
        print("Exiting\n")
        break
                
    else:        
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.OUT)
        time.sleep(0.001)
        GPIO.cleanup()
```

{% file src="https://2255605865-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFgqAozKbevkCPwL6f4%2Fuploads%2FhbeOhWKa9lsYGQ083CG2%2Fpwm_commands.py?alt=media&token=6dc310eb-bc68-4140-9b0f-29d62f5f61a6" %}

The code above allows you to type the letter "t" and then press the "Enter" key and it will take a photo. You can type "s + Enter" and it will unmount the uSD card from the camera and the RasPi will then show it as a removable drive. Typing "s + Enter" again will unmount from RasPi and mount the uSD back to the camera. Typing "e + Enter" exits the program.
