Skip to content

V0.4 Configuring a Raspberry Pi Camera

FormerLurker edited this page Jun 7, 2020 · 11 revisions

Raspberry Pi Camera Configuration Video

Configuring a Raspberry Pi Camera Companion Video

Starting with Octolapse V0.4, the Raspberry Pi camera (tested on the V2 camera) is fully supported. You can now control the camera settings directly within Octolapse!

Prerequisites

Custom image preferences will only work if you are using mjpg-streamer, which is installed by default if you are using OctoPi.

There are a few plugins that create their own camera stream, and these will prevent custom image preferences in Octolapse from working. If you are running one of the following plugins and are having trouble getting custom image preferences working, consider disabling these plugins to see if there is a conflict:

The above plugins are excellent, but they are unfortunately incompatible with custom image preferences currently

Setting up Your Raspberry Pi Camera

Follow the steps below to get your Raspberry Pi Camera (tested with the V2) working with the new custom image preference screen:

Step 1 - Connect to Your Raspberry Pi

If you do not know how to establish a terminal connection to your Raspberry Pi or to any remote Linux instance via SSH, see this guide.

Step 3 - Upgrade Your Raspberry Pi Software

It is very important that your Raspberry Pi is on the latest firmware and is running the latest software/distribution. The required driver does not exist in some older versions.

The following commands may take a while to complete. You will probably need to enter your password (the same one you used to connect) at least once when prompted.

Note: You can paste copied text into Putty with a single right click.

To upgrade your Raspberry Pi, enter each of the following commands followed by the enter key:

sudo apt-get update

Update installed applications

You might be asked to confirm the updates at some point. If so, press Y to confirm.

Next, we will upgrade the distribution. Enter the following command and press the Enter key.

sudo apt-get dist-upgrade

Update your distribution

You might be asked to confirm the updates at some point. If so, press Y to confirm.

Now reboot your Pi for the update to take effect by entering the following command, followed by the Enter key:

sudo reboot

Reboot

The reboot command will reboot your Pi. If you are using Putty, your session will end with an error. Wait a few minutes for your Pi to reboot, then connect again by following Step 1 above. If you are using Putty, you can restart your connection by right-clicking over the title bar (it will say PuTTY (inactive) in the title bar) and selecting restart session. You will be prompted to sign in again if your Pi has booted completely.

Step 4 - Edit Your octopi.txt File

Open your octopi.txt file with the nano editor by entering the following command followed by the Enter key:

sudo nano /boot/octopi.txt

Open octopi.txt in nano

You may be prompted to enter your password. After the command completes, you will see the nano text editor. Use the arrow keys and edit this section (it may look slightly different than what is pictured here, but it should be close):

Default camera settings in octopi.txt

Change the following text towards the top of the file that looks like either #camera="auto" or #camera="raspi" to this:

camera="usb"

Be careful to remove any # characters from the beginning of the line! It should look like this when you are finished:

Octopi.txt configured for a usb camera

Next, using the arrow keys move down to the bottom of the octopi.txt file and find the following section:

Octopi.txt without access to control.htm

change the following text

#camera_http_webroot="./www-octopi"
#camera_http_options="-n"

to this:

camera_http_webroot="./www"
camera_http_options=""

Be careful to remove any # characters at the beginning of the lines! It should look like this when you are finished:

octopi.txt with control.htm access enabled

While you're here, you might want to adjust your camera's resolution and frame rate. Find the camera_usb_options section and edit those now. For example, if you want a 1080P image at 30 frames per second, you would use the following:

camera_usb_options="-r 1920x1080 -f 30"

Make sure to remove any # characters from the beginning of the line!

The end result will look something like this:

1080P at 30 FPS

**Note: The higher the resolution and frame rate, the more bandwidth and CPU power will be used. Higher frame rates will reduce the image acquisition time (possibly improving quality, especially when using a smart trigger with snap-to-print enabled). Also, some versions of Firefox will have trouble showing a high frame rate mjpegstream, but Chrome seems to do just fine. Your mileage may vary, but if you have problems, try lowering your frame rate from 30 frames per second to 10 frames per second.

Now we want to save the file and exit nano. Hold down the ctrl key and press the letter O (so ctrl+O) to save the file. Press enter to confirm. Then press ctrl+X to exit.

Step 5 - Edit the modules file and add the Raspberry Pi camera driver

Next we will add the Raspberry Pi Camera Driver to your modules file, which will cause it to be loaded at boot.

Open up your modules file in the nano editor with the following command followed by the Enter key:

sudo nano /etc/modules

Open modules file in the nano editor

You should see something similar to the following:

Contents of the /etc/modules file

At the very bottom of the file add the following text:

bcm2835-v4l2

When you are finished, your /etc/modules file should look like this:

ect/modules file with the Raspberry Pi camera driver added

Hold down the ctrl key and press the letter O (so ctrl+O) to save the file. Press enter to confirm. Then press ctrl+X to exit.

Finally, reboot your Pi by entering the following command followed by the Enter key:

sudo reboot

Reboot

You may need to enter your password again if prompted. If you are using Putty, your session will end with an error. Wait until your Raspberry Pi has rebooted completely and then verify that OctoPrint is ready by opening it in a new browser window.

Step 6 - OPTIONAL - Add a New Camera Profile

If you don't want to change the settings of an existing camera profile (perhaps because you have multiple cameras), the steps below will walk you through adding a new camera profile. If you only have one camera, you can skip this step and move on to Step 7

Open up OctoPrint in a browser window and click on the Octolapse tab. Make sure the Current Run Configuration is expanded. If they are not visible, click on the Current Settings link.

Then open up the camera profiles page within the Octolapse settings by clicking on the 'Cameras' link:

Open the camera profiles page

Next click on the Add Profile... button.

Add a new camera profile

Name your profile Raspi Camera, or something similar, and click on the Save button.

Name and save your new camera profile

Now exit the OctoPrint settings popup by clicking Cancel at the bottom right hand corner.

Close Octolapse settings popup

Step 7 - Editing and Configuring your Raspberry Pi Camera Profile

Click the edit icon next to your your Raspberry Pi camera profile.

Edit your raspberry pi camera profile

This will open up the existing camera profile settings. If you want to rename your profile, feel free to do so now.

Next, make sure that you are not using an imported profile by selecting `Not Selected' from the dropdown box.

Make sure "no profile to import" is selected

Make sure that the Camera Type dropdown box is set to Webcam. Your settings will look something like this:

Camera type and address

Now we need to verify the Snapshot Address Template. Make sure your camera can take snapshots by clicking on the 'Test' button. If it is successful, it will look like this:

Camera test success!

If the test failed, you will see this message:

Camera test failed :(

If your test has failed, see this troubleshooting guide for help getting the Snapshot Address Template working.

Step 8 - Enabling Custom Image Preferences

Find the Custom Image Preferences section within the camera profile and check the Enabled check box:

Enable Custom Image Preferences

If you see an error like this, follow the troubleshooting guide here:

Enable custom image preferences failed

If custom image preferences were successfully enabled, you will see three new options:

  • Apply Preferences at Startup - When this option is enabled Octolapse will apply the saved image preferences after OctoPrint boots. I've gotten a few reports that some USB cameras take a long time to come online, preventing Octolapse from applying the settings after a reboot. I've since altered the routine so that Octolapse will retry for a minute or two.
  • Apply Preferences Before Print Start - Enabling this option will cause Octolapse to apply the camera settings at the start of a print.
  • Apply Preferences When Camera is Disabled. You can disable cameras selectively within Octolapse, but you may still want your custom image preferences applied. When this option is enabled, Octolapse will still apply settings to disabled cameras according to the Apply Preferences at Startup and Apply Preferences Before Print Start settings.

All three should be enabled by default. Your settings should now look like this:

Custom Image Preferences are enabled and will be applied after boot and before a print starts, even if the camera profile is disabled

Now, save your profile by clicking the Save button:

Save camera custom image preferences

Next, select your timelapse preview camera in the dropdown on the Octolapse tab found here:

Select the raspberry pi camera as your current timelapse preview camera

You will now see a message explaining that no snapshots are available for the Raspberry Pi camera. This is normal, and you won't see any snapshots here until you run a print with Octolapse enabled.

Next, click the edit pen to the right of the camera you just selected to open the image preferences:

Edit the custom image preferences for your Raspberry Pi camera

If everything is working properly, you will be able to see your camera stream at the top of the popup, which will look similar to this:

An example of a working camera stream

If the stream is not working, you will see something like this:

This camera stream failed to load

If your camera stream will not load, see this troubleshooting guide.

Finally, save your profile by clicking on the Save button at the very bottom.

Step 9 - Editing Your Camera's Image Preferences

Dialing in your camera settings is very important for getting a high quality timelapse. Before you start, it is important that you have adequate lighting. Good lighting is critical for getting a high quality timelapse, and it might even improve your print quality by reducing the snapshot acquisition time! Photography LEDs work great, but cheap LEDs that use low frequency pulse width modulation (PWM) might result in a lot of flicker in your final timelapse. Incandescent and halogen bulbs will work well, but they use a lot more power than LED lights. In general, you will want your lighting to be much brighter than is necessary to just see your prints.

I also recommend that you close any blinds in the room with the printer because the light levels in the room may vary, messing up the exposure and leading to a lower quality timelapse.

Once you have improved your lighting, it's time to adjust the camera settings.

  1. Camera Stream - Here you will find your live camera stream. Any changes you make to your camera settings will appear here after a brief delay.
  2. Stabilize Extruder - This will first home your extruder's X and Y axis according to your printer profile's *Home Axis Gcode Script *. Next your extruder should move into the snapshot position. This should make it easier to focus your camera properly for your timelapse. If you are using one of the the Snap to Print smart triggers or if stabilization is disabled, this will not work.
  3. Apply All Settings Now - Octolapse must remain in sync with your camera, but sometimes things go wrong. For example, if you change your camera settings with another application or if you disconnect and reconnect it after booting your Pi, the settings might not be applied to your camera. Clicking this button will force all settings to be applied to your camera.
  4. Switch to Default Page/Switch to Custom Page - A few cameras have special custom pages associated with them. Currently, several Logitech webcams and Raspberry Pi cameras have custom pages that are enhanced with grouping, validation, and other features that enhance usability. This button can be used to switch between the two if a custom page is available. If the button says Switch to Custom Page but no page is available, you will see an error message.
  5. Switch to Single Column/Switch to Two Columns - By default, your controls will be shown as two newspaper style columns. If you prefer a wider format, you can switch to single column mode, which can make it a bit easier to control the sliders.
  6. Specific Camera Detected - If Octolapse knows what kind of camera you are using, it will be shown here. Octolapse detects cameras based on their feature set and default settings, and these can change with driver or mjpg-streamer updates. If Octolapse cannot detect your camera type, you won't have access to a Custom Control Page.
  7. Cancel - This will undo all of the changes that were made since you opened the popup, but only if your camera settings were properly synchronized when you opened the popup (see item 3).

Important Note: The Raspberry Pi controls can be finicky. Cancel will not always be able to reset your image because of glitches in the controls. See Settings You Should Probably Not Use below for more details.

  1. Defaults - Here you can restore your camera's default settings. Please note that some cameras do not properly report the default settings, so this may not work perfectly for all cameras or versions of mjpg-streamer.

Important Note: The Raspberry Pi controls can be finicky. Defaults will not always be able to reset your image because of glitches in the controls. See Settings You Should Probably Not Use below for more details.

  1. Save - If you are happy with your settings adjustments, click save to store them.

There are three types of controls you may see within the image preferences popup:

  1. Sliders - All settings that are associated with numeric controls will appear as a slider. Each slider has a minimum and maximum value as well as a step. For example, my camera allows the focus to be set between 0 and 250 in increments of 5. Because of this, a value of -10 or 32 is invalid, so the slider will not allow you to select these values. Note: A few people have asked for the option to use a text box to manually enter a value. This is not possible at the moment, but I am considering adding this feature.
  2. Check Boxes - These are toggle controls that can be on or off.
  3. Dropdown Boxes - These contain a list of possible values that you may choose from.

Sometimes the camera controls interact with one another. For example, the Exposure Time, Absolute setting will only work if Scene Mode is set to None and Auto Exposure is set to Manual Mode. The custom page (if they are available) will usually make this a bit easier to figure out by enabling some controls based on other settings. Custom pages also contain help links (blue question marks) next to each setting, which you can use to learn more about each control. The default pages do NOT do this.

Step 10 - Dialing in Your Camera

I recommend spending a lot of time tuning your camera settings. Some settings are more important than others, and your settings may need to change based on lighting, camera position, the color of the filament you are printing with, etc., so they will need regular adjustments. I pretty regularly see people asking what settings they should use, but it's very unlikely that my settings will work for you, even if we have the exact same camera. For this reason, it's easier to explain my method for adjusting my own camera settings. That way you can come up with the best settings for your unique situation.

Adjust Your Lighting

This step is critical to getting good image quality. A good rule of thumb is that you should use more lighting than you think is necessary. You should also make sure that the lighting conditions will not change during your print, so I recommend closing the blinds and shutting the doors. Avoid turning lights on and off during your print or else you will definitely notice this in your video.

Position Your Camera

This is a bit more art than science. Ideally, you want to choose an angle that will show the entire print. Play around with your positioning and see what works best for you.

Adjust the Video Bitrate

For the Raspberry Pi camera, the first thing you should do is increase the Video Bitrate. Increase the value by moving the slider next to this control all the way to the right until it looks similar to this:

Codec controls with video bitrate maxed

You should notice an immediate improvement in your image quality. Try moving the slider slowly up and down to see the effect. Note that higher bitrates mean more bandwidth will be used, so keep that in mind.

Adjust Your Focus

Now it's time to adjust the focus. By default the Raspberry Pi camera is set to focus on the horizon, which means items that are up close will appear to be blurry. This is not ideal for 3D printing, since your camera will most likely be very close to your printer. You should check your focus any time you adjust your stabilization position or if you move your camera.

In order to get the focus right, it's important that your bed and extruder are in the right position. Fortunately, Octolapse can stabilize your bed on demand, making this pretty easy.

Important Note: Octolapse cannot stabilize your bed if you are using any of the Snap to Print triggers or if stabilization is disabled. In this case it may be a bit trickier to focus your camera because the stabilization position is unknown until after the snapshot plan is generated. Additionally, for Animated stabilizations, Octolapse will stabilize to the first position in the animation.

Before you attempt to stabilize your extruder, make sure that your bed is clear! If not, your extruder may crash into whatever is sitting on your bed, potentially damaging your printer. Octolapse will ask you to check your bed before sending any Gcode to your printer.

Once you have checked your bed to make sure it is totally empty, click on the Stabilize Extruder button:

Click on the Stabilize Extruder Button

Octolapse will now ask you to confirm that you want to home and stabilize the XY axis. Click Ok only if your bed is clear. Your printer should immediately home and then move into the stabilization position:

custom_image_preferences_stabilize_extruder_confirm

In order to make it a bit easier to adjust the focus, I recommend putting an object on your bed in the same position that your printed object will be located. Preferably this object has some text on it because I find that it's easier to tell if an object is in sharp focus if it has text on it. You may need to raise your Z axis in order to fit an object on the bed depending on where your extruder is. Use the OctoPrint control tab for this:

Raise your Z-Axis

First select the Control tab. You can move your Z axis using these controls:

  1. Raise your extruder
  2. Lower your extruder
  3. Control how far your extruder will move in MM.

Important Note: Some printers move the entire bed when the Z axis is changed. If your printer is like this and you find that your controls are flipped (up appears to lower your bed and down appears to raise it), you can correct this by editing your OctoPrint (NOT Octolapse) printer profile, enabling the Invert control option to correct this:

Invert your Z-Axis Controls if They Appear to be Flipped

Now place the object of your choice on your bed and adjust the focus until it is sharp. It can be a bit of a pain to adjust the focus on a Raspberry Pi Camera, but there are numerous guides and videos on the internet explaining how to do this. Please note that the exact model of your camera will affect how you adjust the focus (v1, v2, etc...). I will link to a good guide for this at some point.

Adjust your Exposure

Next, adjust your exposure. Exposure controls how much light will reach your camera sensor. Automatic exposure can cause flickering in your timelapse, so I recommend using manual settings. To enable manual exposure, start by setting the Scene Mode to None. Then select Manual Mode from the Auto Exposure dropdown box. You will now be able to adjust the Exposure Time, Absolute setting. You want to use as low an exposure setting as possible while maintaining good image quality. Here is an example of what you might end up with (your exact settings might vary):

Manual exposure with a low exposure time

Important Note - At least for the version of the Raspberry Pi camera that I have, manual exposure only seems to have an affect between 1 and about 350. Anything above 350 seems to look just like the 350 setting.

Adjust the Red/Blue Balance

Auto balance can cause flickering, so I recommend using manual balance. First you need to enable manual balance:

Important Note: There is currently a bug in the USB driver for controlling the Red/Blue balance. Once you switch to Manual mode, you cannot switch to any other mode without switching back to Auto, saving your settings, then rebooting the camera. This is unfortunate, especially since getting the red/blue balance right is very tricky and time consuming. If you have figured out a way to do this without a reboot, please let me know!

Select Manual Balance

Once manual balance is enabled you can control the red and blue channels individually. It is a bit tricky to adjust the balance, so expect to spend quite a bit of time getting this right. Here are the settings I came up with for my camera:

Final Red/Blue Balance

Other Interesting Settings

Other obvious settings you may want to look at include Brightness, Contrast, Saturation, Sharpness, Flip/Rotate Image, and Power Line Frequency. Your camera may not support all of these controls and may have other interesting things you may want to try.

  • Brightness - Controls how bright or dark your image will be. This is independent of the Exposure setting discussed above and should be adjusted after you set your exposure.
  • Contrast - This controls the difference between the brightest and darkest parts of your image. A higher contrast means that shadows and highlights will be more clearly defined. A lower contrast makes the image appear a bit flat.
  • Saturation - This is the lightness or darkness of a specific range of colors. Some cameras have separate saturation settings for the red, green, and blue (or CYMK) channels, and some have a single saturation setting that applies to all colors evenly. Increasing this will make colors appear more vibrant but less true-to-reality. Lowering the setting will eventually remove all colors from your image, leaving you with a black and white image.
  • Sharpness - This setting enhances brightness along edges. It can make features more defined at higher settings, but it may create an undesirable halo or jagged edges if it gets too high.
  • Flip/Rotate Image - Here you can flip your image horizontally or vertically. You can also rotate your image in increments of 90 degrees. All three controls can be combined, so you can flip your image both horizontally and vertically, and then rotate the result.
  • Power Line Frequency - The frequency of your power may cause flickering in your image. Many cameras have built-in correction for this. Check out this link to find out what frequency is used in your area, and select the appropriate frequency for this setting if it is available.

Settings You Should Probably Not Use

The Raspberry PI controls are are finicky and a bit buggy. I'm leaving the Balance setting out of this list, since it really should be set manually, though you cannot change back to auto settings through Octolapse once you set it to manual (discussed above).

Here is a potentially incomplete list of controls with bugs severe enough that I don't recommend you change them:

  1. Exposure - Image Stabilization - Once this setting is enabled, your camera will appear to zoom in slightly, which is required for image stabilization to work. However, you cannot disable the setting without a reboot, and it is pretty much useless for timelapses anyway because the stabilization itself will cause a jerky looking timelapse.
  2. Effects - I recommend you leave this set to None. Some of the effects work as expected, but others seem to affect your image until reboot.

Congratulations! Your Raspberry Pi Camera is all configured!

Clone this wiki locally