# Raspberry Pi Low-Light Long-Exposure Photography

## Overview

Have you ever wished you could take pictures at night with the sweet Raspberry Pi High Quality (HQ) Camera?

Using the _raspistill_ command line program is tricky because the auto setting determination usually results in a black picture.

Now, though, with a little Python, you can take pictures up to six seconds long at ISO 800! That'll turn a dark night lit only by a few house lights into a bright, well-saturated picture!

![](https://cdn-learn.adafruit.com/assets/assets/000/102/100/medium800/camera_202105090247_kh_low.jpg?1620781787)

_This image was taken at one in the morning with a Pi4 and HQ Camera with 6mm lens._

There are a wide range of options you can use to implement this. I wanted a compact, portable system to do astrophotography, so I built the system specifically to do that. If you want, you can just use the code with an HQ Camera setup you already have. It's totally up to you how sophisticated you want to be. (For example, I don't go into motorized star tracker equatorial mounts even though I'm doing astrophotography.) The mounting solution is up to you, again, depending on your use cases!

## Parts
### Raspberry Pi 4 Model B - 4 GB RAM

[Raspberry Pi 4 Model B - 4 GB RAM](https://www.adafruit.com/product/4296)
The Raspberry Pi 4 Model B is the newest Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 4 better than the 3? How about a&nbsp;_faster_ processor, USB 3.0 ports, and updated Gigabit Ethernet chip with...

In Stock
[Buy Now](https://www.adafruit.com/product/4296)
[Related Guides to the Product](https://learn.adafruit.com/products/4296/guides)
![Angled shot of Raspberry Pi 4](https://cdn-shop.adafruit.com/640x480/4296-11.jpg)

### Raspberry Pi High Quality HQ Camera

[Raspberry Pi High Quality HQ Camera](https://www.adafruit.com/product/4561)
Snap, snap! There's a new official camera board released by the Raspberry Pi Foundation!

The **Raspberry Pi High Quality Camera** is the latest camera accessory from Raspberry Pi. It offers higher resolution ( **12.3 megapixels** , compared to 8 megapixels),...

In Stock
[Buy Now](https://www.adafruit.com/product/4561)
[Related Guides to the Product](https://learn.adafruit.com/products/4561/guides)
![Top view of Raspberry Pi HQ camera module.](https://cdn-shop.adafruit.com/640x480/4561-18.jpg)

### 16mm 10MP Telephoto Lens for Raspberry Pi HQ Camera

[16mm 10MP Telephoto Lens for Raspberry Pi HQ Camera](https://www.adafruit.com/product/4562)
This is the 1 **6mm 10MP Telephoto&nbsp;Lens** , an essential for&nbsp;the [Raspberry Pi High Quality Camera](http://www.adafruit.com/product/4561). It has a wide field of view and a variety of uses, like CCTV applications, or&nbsp;of course, cat photos. Telephoto lenses...

In Stock
[Buy Now](https://www.adafruit.com/product/4562)
[Related Guides to the Product](https://learn.adafruit.com/products/4562/guides)
![Angled Shot of the 16mm 10MP Telephoto Lens for Raspberry Pi HQ Camera (1). Positioned Vertically.](https://cdn-shop.adafruit.com/640x480/4562-03.jpg)

### Pimoroni Mini Black HAT Hack3r - Fully Assembled

[Pimoroni Mini Black HAT Hack3r - Fully Assembled](https://www.adafruit.com/product/3182)
A nifty little breakout from our friends at [Pimoroni](http://shop.pimoroni.com/)&nbsp;that lets you access all of the GPIO pins while also running a [HAT or pHAT](https://www.adafruit.com/categories/286) on your

Out of Stock
[Buy Now](https://www.adafruit.com/product/3182)
[Related Guides to the Product](https://learn.adafruit.com/products/3182/guides)
![Angled shot of Pimoroni Mini Black HAT Hack3r](https://cdn-shop.adafruit.com/640x480/3182-01.jpg)

### Part: 10000 mAh Li-Ion Battery Pack
quantity: 1
USB Battery Pack for Raspberry Pi - 10000 mAh
[10000 mAh Li-Ion Battery Pack](https://www.adafruit.com/product/1566)

### Part: USB A to USB C Cable 
quantity: 1
USB Type A to Type C Cable - approx 1 meter / 3 ft long
[USB A to USB C Cable ](https://www.adafruit.com/product/4474)

### Part: Stacking GPIO Header
quantity: 1
Stacking 2x20 Extra Tall Header
[Stacking GPIO Header](https://www.adafruit.com/product/1979)

### Part: Keyboard
quantity: 1
Official Raspberry Pi Keyboard
[Keyboard](https://www.adafruit.com/product/4112)

### Part: Mouse
quantity: 1
Official Raspberry Pi Mouse
[Mouse](https://www.adafruit.com/product/4113)

### Part: Male/Female Jumper Wires
quantity: 1
Premium Male-Female Jumper Wires, 3 inches
[Male/Female Jumper Wires](https://www.adafruit.com/product/1953)

### Part: ICE Tower Cooling System
quantity: 1
GeeekPi Raspberry Pi 4 Cooling Tower
[ICE Tower Cooling System](https://www.amazon.com/GeeekPi-Raspberry-Cooling-Cooler-Heatsink/dp/B07V35SXMC/ref=sr_1_2?dchild=1&keywords=geeekpi+ice+tower&qid=1621253001&sr=8-2)

### Part: M2.5 Brass Standoff Kit
quantity: 1
M2.5 Brass Standoff Kit
[M2.5 Brass Standoff Kit](https://www.amazon.com/LBY-Multi-function-Combination-Screwdriver-Green/dp/B07GK6812D/ref=sr_1_3?dchild=1&keywords=M2.5+brass+standoff&qid=1621253132&sr=8-3)

### Part: Raspberry Pi Mounting Plate Pro
quantity: 1
Raspberry Pi Mounting Plate for High Quality Camera
[Raspberry Pi Mounting Plate Pro](https://thepihut.com/products/mounting-plate-for-high-quality-camera?variant=31867507146814)

### Part: 128GB Flash Drive
quantity: 1
Samsung 128GB Flash Drive
[128GB Flash Drive](https://www.amazon.com/Samsung-MUF-128AB-AM-Plus-128GB/dp/B07D7PDLXC/ref=sr_1_3?dchild=1&keywords=samsung+128gb+flash+drive&qid=1621253528&sr=8-3)

# Raspberry Pi Low-Light Long-Exposure Photography

## Assembly

Collect all the parts in one place, and it's time to start the build. (You'll notice some of the parts in the picture are already partially assembled.)

![](https://cdn-learn.adafruit.com/assets/assets/000/102/101/medium800/camera_IMG_4061.jpg?1620784007)

First, attach the included ribbon cable to the HQ Camera, or use one you have handy. Then attach the HQ Camera to the HQ Camera Mount Pro. You can put the lens on now, or wait a while, whichever you prefer. It doesn't get in the way. The official HQ Camera Mount Pro assembly instructions&nbsp;[are available here](https://thepihut.com/HQmountPro)&nbsp;(they're helpful for finding the right bolts to use).

![camera_IMG_4062.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/102/medium640/camera_IMG_4062.jpg?1620784492)

Attach the camera ribbon cable to the Pi 4. Remember, the silver connectors face&nbsp;_towards_&nbsp;the HDMI ports on the Pi.

![camera_IMG_4063.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/103/medium640/camera_IMG_4063.jpg?1620784814)

Attach the ICE Tower to the Raspberry Pi 4 following the enclosed instruction booklet. Leave off the lower base plate, though, since that's where we'll attach the camera mount.

![camera_IMG_4064.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/104/medium640/camera_IMG_4064.jpg?1620784901)

Now take a break and use the Raspberry Pi Imager on a separate computer to burn the Raspberry Pi OS onto the flash drive. I recommend using the 32-bit release. The Pi 4 now supports booting from USB, so this will work with new Pi 4s. If you have an older Pi 4 without the updated firmware, you'll need to follow a procedure to update the firmware yourself (not documented here). Spoiler: it's a lot easier to use a new Pi 4.

&nbsp;

Once the flash drive is ready, put it in one of the two USB-C ports on the Pi 4. Those are the ones with the blue spacers.

![camera_IMG_4065.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/105/medium640/camera_IMG_4065.jpg?1620786370)

Now that the bits are in place, we can go back to assembling.

&nbsp;

Connect three 20mm brass standoffs together so they are long enough for the Hack3r plate to clear the fan. Make two of these.

![camera_IMG_4066.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/106/medium640/camera_IMG_4066.jpg?1621254571)

Screw those standoffs onto the two ICE Tower mounting bolts&nbsp;_opposite_&nbsp;the GPIO headers.

![camera_IMG_4067.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/107/medium640/camera_IMG_4067.jpg?1620787144)

Things are coming together now! Press the tall Pi headers into one end of the Black Hat Hack3r ribbon cable. This provides the clearance to connect the Hack3r to the Pi.

![camera_IMG_4068.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/108/medium640/camera_IMG_4068.jpg?1620787996)

Attach the other end of the cable to the Hack3r plate.

![camera_IMG_4070.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/109/medium640/camera_IMG_4070.jpg?1621254399)

Attach the Hack3r plate to the standoffs in the orientation shown.

![camera_IMG_4071.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/110/medium640/camera_IMG_4071.jpg?1620789049)

Next, attach the Hyperpixel display to the Hack3r panel. It will protrude to the left in the above picture. Be very careful not to press down on the glass. Grasp the PCB at the sides and slowly wiggle it onto the pins. You can see the final position here.

![camera_IMG_4073.jpg](https://cdn-learn.adafruit.com/assets/assets/000/102/111/medium640/camera_IMG_4073.jpg?1621254222)

Last assembly step! Use a red and a black wire to extend the fan power connections to the Hack3r plate header row that's still open.

That's it! Now you have a camera that, with the power bank and USB-C cable, is portable to the locations you want to shoot.

# Raspberry Pi Low-Light Long-Exposure Photography

## Software Configuration

Step one is to [configure the operating system](https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up/) on the Pi. Connect the external monitor, and mouse and keyboard. Then apply power and configure as you normally would for a Raspberry Pi. The Pi should boot automatically from the flash drive.

Follow the on-screen instructions. When you have the install done, run Preferences-\>[Raspberry Pi Configuration](https://projects.raspberrypi.org/en/projects/raspberry-pi-using/9). I recommend changing the Pi's name. Then configure the interfaces. Be sure to enable SPI, I2C, and Camera.

![](https://cdn-learn.adafruit.com/assets/assets/000/102/145/medium800/camera_interface_config.png?1620947179)

When you click OK you should reboot the Pi.

Step two is to configure the Hyperpixel 4 display. [Run this command](https://projects.raspberrypi.org/en/projects/raspberry-pi-command/1) as the user pi, not root (i.e., don't use sudo):

```none
curl https://get.pimoroni.com/hyperpixel4 | bash
```

When it runs, it will ask for your display. Pick the correct one. If you are following this guide to the letter, you'll want the Pi 4 Square option.

When the script has finished running, it will ask if you want to reboot. Say yes.

Step three is to make the system usable. The display will be upside down when the Pi comes back up, which makes it a bit difficult to use. Fortunately, there is a command that will fix it. You'll only need this once:

```none
hyperpixel4-rotate inverse
```

Now the display will be right side up and you can use it like any other Raspberry Pi.

## Taking Pictures

Copy the following text to a file called **lowlight.py**. You can click the **Download Project Bundle** link to get a zip file containing the Python file:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/RasPi_Low_Light_Camera/lowlight.py

Run the program on your py to take a picture. It will save a jpg in the current directory with a timestamp.

$ ./lowlight.py

Adjust the shutter speed to get the exposure you want.

# Raspberry Pi Low-Light Long-Exposure Photography

## Python Code

Open up your favorite editor and save the following code as **lowlight.py**

First we import the libraries we need. You don't need to install anything special since these libraries are included with Raspberry Pi OS.

```python
from picamera import PiCamera
import time
from fractions import Fraction
import datetime
```

Create the timestamp string used to build the output file name.

```python
cur_time = datetime.datetime.now()
stub = cur_time.strftime("%Y%m%d%H%M_low")
```

Next, create an instance of the `Camera` object. This is where the magic is, since it allows us to change the default frame rate to 1/6 of a second.

```python
camera = PiCamera(framerate=Fraction(1,6))
```

Now we can set the shutter speed and ISO in the `Camera` object instance. The shutter speed is in milliseconds, so for a six second exposure you would use 6000000.

```python
# You can change these as needed. Six seconds (6000000)
# is the max for shutter speed and 800 is the max for ISO.
camera.shutter_speed = 1750000
camera.iso = 800
```

Next, we let the camera collect available light to auto-detect other camera settings. We also turn off the exposure mode so the auto-detection won't change our shutter speed and frame rate.

```python
time.sleep(30)
camera.exposure_mode = 'off'
```

Now we set the output file name and take a picture!

```python
outfile = "%s.jpg" % (stub)
camera.capture(outfile)
```

The last step is to close the `Camera` object so the operating system properly releases the camera controls.

```python
camera.close()
```

### Full Example Code
```python
from picamera import PiCamera
import time
from fractions import Fraction
import datetime

cur_time = datetime.datetime.now()
stub = cur_time.strftime("%Y%m%d%H%M_low")

camera = PiCamera(framerate=Fraction(1,6))

# You can change these as needed. Six seconds (6000000)
# is the max for shutter speed and 800 is the max for ISO.
camera.shutter_speed = 1750000
camera.iso = 800

time.sleep(30)
camera.exposure_mode = 'off'

outfile = "%s.jpg" % (stub)
camera.capture(outfile)

camera.close()
```

# Raspberry Pi Low-Light Long-Exposure Photography

## Future Work

There are some things we could do to enhance the project.

1. To enhance portability, we can configure the Raspberry Pi as a WiFi access point. This would create a local wireless network so you can control the system with an iPad with a VNC Client installed.
2. Modify **lowlight.py** so you can specify the shutter speed and ISO with options.
3. Build a small LED light and use it for light painting.
4. Use velcro and piggyback it on a motorized telescope, then do astrophotography.

Use your imagination!


## Featured Products

### Raspberry Pi 4 Model B - 4 GB RAM

[Raspberry Pi 4 Model B - 4 GB RAM](https://www.adafruit.com/product/4296)
The Raspberry Pi 4 Model B is the newest Raspberry Pi computer made, and the Pi Foundation knows you can always make a good thing _better_! And what could make the Pi 4 better than the 3? How about a&nbsp;_faster_ processor, USB 3.0 ports, and updated Gigabit Ethernet chip with...

In Stock
[Buy Now](https://www.adafruit.com/product/4296)
[Related Guides to the Product](https://learn.adafruit.com/products/4296/guides)
### Raspberry Pi High Quality HQ Camera

[Raspberry Pi High Quality HQ Camera](https://www.adafruit.com/product/4561)
Snap, snap! There's a new official camera board released by the Raspberry Pi Foundation!

The **Raspberry Pi High Quality Camera** is the latest camera accessory from Raspberry Pi. It offers higher resolution ( **12.3 megapixels** , compared to 8 megapixels),...

In Stock
[Buy Now](https://www.adafruit.com/product/4561)
[Related Guides to the Product](https://learn.adafruit.com/products/4561/guides)
### 16mm 10MP Telephoto Lens for Raspberry Pi HQ Camera

[16mm 10MP Telephoto Lens for Raspberry Pi HQ Camera](https://www.adafruit.com/product/4562)
This is the 1 **6mm 10MP Telephoto&nbsp;Lens** , an essential for&nbsp;the [Raspberry Pi High Quality Camera](http://www.adafruit.com/product/4561). It has a wide field of view and a variety of uses, like CCTV applications, or&nbsp;of course, cat photos. Telephoto lenses...

In Stock
[Buy Now](https://www.adafruit.com/product/4562)
[Related Guides to the Product](https://learn.adafruit.com/products/4562/guides)
### Pimoroni HyperPixel 4.0 Square - Hi-Res Display for Raspberry Pi

[Pimoroni HyperPixel 4.0 Square - Hi-Res Display for Raspberry Pi](https://www.adafruit.com/product/4498)
It's hip to be square with the Pimoroni's **HyperPixel 4.0 Square Display for Raspberry Pi computers**. This display has all the great features of their [standard HyperPixel 4.0](https://www.adafruit.com/?q=hyperpixel%20800x480)—a crisp, brilliant IPS...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/4498)
[Related Guides to the Product](https://learn.adafruit.com/products/4498/guides)
### Pimoroni Mini Black HAT Hack3r - Fully Assembled

[Pimoroni Mini Black HAT Hack3r - Fully Assembled](https://www.adafruit.com/product/3182)
A nifty little breakout from our friends at [Pimoroni](http://shop.pimoroni.com/)&nbsp;that lets you access all of the GPIO pins while also running a [HAT or pHAT](https://www.adafruit.com/categories/286) on your

Out of Stock
[Buy Now](https://www.adafruit.com/product/3182)
[Related Guides to the Product](https://learn.adafruit.com/products/3182/guides)
### Premium Female/Male 'Extension' Jumper Wires - 40 x 3" (75mm)

[Premium Female/Male 'Extension' Jumper Wires - 40 x 3" (75mm)](https://www.adafruit.com/product/825)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are approximately 3" (75mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on one end and 0.1" female...

In Stock
[Buy Now](https://www.adafruit.com/product/825)
[Related Guides to the Product](https://learn.adafruit.com/products/825/guides)
### USB Battery Pack for Raspberry Pi - 10000mAh - 2 x 5V outputs

[USB Battery Pack for Raspberry Pi - 10000mAh - 2 x 5V outputs](https://www.adafruit.com/product/1566)
A large-sized rechargeable battery pack for your [Raspberry Pi](http://www.adafruit.com/raspberrypi) (or [Arduino](http://www.adafruit.com/arduino), or [Propeller](http://www.adafruit.com/category/26), or anything else that uses 5V!). This pack is intended for...

In Stock
[Buy Now](https://www.adafruit.com/product/1566)
[Related Guides to the Product](https://learn.adafruit.com/products/1566/guides)
### USB Type A to Type C Cable - approx 1 meter / 3 ft long

[USB Type A to Type C Cable - approx 1 meter / 3 ft long](https://www.adafruit.com/product/4474)
As technology changes and adapts, so does Adafruit. This&nbsp;&nbsp; **USB Type A to Type C** cable will help you with the transition to USB C, even if you're still totin' around a USB Type A hub, computer or laptop.

USB C is the latest industry-standard connector for...

In Stock
[Buy Now](https://www.adafruit.com/product/4474)
[Related Guides to the Product](https://learn.adafruit.com/products/4474/guides)

## Related Guides

- [Raspberry Pi Care and Troubleshooting](https://learn.adafruit.com/raspberry-pi-care-and-troubleshooting.md)
- [Narrative Clip Teardown](https://learn.adafruit.com/narrative-clip-teardown.md)
- [Raspberry Pi Wearable Time Lapse Camera](https://learn.adafruit.com/raspberry-pi-wearable-time-lapse-camera.md)
- [Raspberry Pi Selfie Bot](https://learn.adafruit.com/raspberry-pi-selfie-bot.md)
- [Using a Raspberry Pi as a Router](https://learn.adafruit.com/using-a-raspberry-pi-as-a-router.md)
- [3D Scans for Low Poly Statues](https://learn.adafruit.com/low-poly-3d-scans-for-3d-printing.md)
- [Adding a Single Board Computer to Blinka](https://learn.adafruit.com/adding-a-single-board-computer-to-blinka.md)
- [I2C/SPI LCD Backpack](https://learn.adafruit.com/i2c-spi-lcd-backpack.md)
- [Adding a Single Board Computer to PlatformDetect for Blinka](https://learn.adafruit.com/adding-a-single-board-computer-to-platformdetect-for-blinka.md)
- [Articulated Pi Display V2 Mount](https://learn.adafruit.com/pi-wall-mount.md)
- [CircuitPython with Jupyter Notebooks](https://learn.adafruit.com/circuitpython-with-jupyter-notebooks.md)
- [Adafruit OV7670 Camera Library For SAMD51 Processors](https://learn.adafruit.com/adafruit-ov7670-camera-library-samd51.md)
- [Adafruit GPIO Expander Bonnet for Raspberry Pi](https://learn.adafruit.com/gpio-expander-bonnet.md)
- [Internet Streaming "On Air" Sign](https://learn.adafruit.com/internet-streaming-on-air-sign.md)
- [Desktop or Laptop TFT Sidekick With FT232H](https://learn.adafruit.com/tft-sidekick-with-ft232h.md)
- [Character LCDs](https://learn.adafruit.com/character-lcds.md)
