Switching To An Lcd Monitor On Raspberry Pi

how to switch to lcd monitor on pi

You can switch between an LCD monitor and an HDMI monitor on a Raspberry Pi by using a script to switch between the LCD pi hat and an external HDMI. This can be done by disconnecting the HDMI cable and rebooting the device.

Characteristics Values
Display type LCD
Display size 3.5"
Resolution 320x240
Touchscreen Yes
Connection type HDMI

shundigital

Switch between LCD and HDMI using a script

To switch between an LCD and HDMI display on a Raspberry Pi, you can use a script to automate the process. Here's a step-by-step guide:

Step 1: Create Config Files

Create two unique config files, such as "config_lcd.txt" and "config_hdmi.txt," in the "/boot" folder. These files will contain the display settings for your LCD and HDMI displays, respectively. The specific settings will depend on your hardware configuration.

Step 2: Create a Shell Script

Create a shell script, such as "initDisplay.sh," in a directory of your choice. This script will check for the presence of an HDMI connection and copy the appropriate config file to "/boot/config.txt," which controls the display output. Give the script full permissions using the command:

Bash

Chmod 777 initDisplay.sh

Step 3: Script Contents

Here's an example of what your "initDisplay.sh" script might look like:

Bash

#!/usr/bin/env bash

Check for HDMI connection

Rm -f hdmi.name

Tvservice -n 2> hdmi.name

HDMI_NAME=$(cat hdmi.name)

Check if HDMI is connected

If [ "$HDMI_NAME" == "[E] No device present" ]; then

# Check if LCD is currently enabled

LCD_ON=$(grep "hdmi_drive=2" /boot/config.txt)

If [ "$LCD_ON" == "hdmi_drive=2" ]; then

# Enable HDMI and reboot

Sudo rm -f /boot/config.txt

Sudo cp /boot/config_hdmi.txt /boot/config.txt

Sudo reboot -n

Fi

Else

# Check if HDMI is currently enabled

HDMI_ON=$(grep "lcd_rotate=2" /boot/config.txt)

If [ "$HDMI_ON" == "lcd_rotate=2" ]; then

# Enable LCD and reboot

Sudo rm -f /boot/config.txt

Sudo cp /boot/config_lcd.txt /boot/config.txt

Sudo reboot -n

Fi

Fi

Step 4: Modify Boot Script

Modify the boot script in "/etc/profile.d/" to execute the display script before anything else. For example, if you have a file called "10-retropie.sh," it might look like this:

Bash

Check display configuration

/home/pi/initDisplay.sh

Sleep 5

Launch autostart apps

If [ "`tty`" = "/dev/tty1" ]; then

Bash "/opt/retropie/configs/all/autostart.sh"

Fi

Step 5: Run the Script

To switch between LCD and HDMI, simply run the display script. The script will check for the display connection, update the "/boot/config.txt" file, and reboot the Raspberry Pi with the selected display.

Note: This is just one example of a script to switch between LCD and HDMI. You can also explore other methods, such as using the "tvservice" command or modifying the config file manually.

shundigital

Change the config.txt file to force output to HDMI

To change the config.txt file to force output to HDMI, you can follow these steps:

  • Locate the config.txt file: The config.txt file is typically found in the /boot/firmware/ directory on your Raspberry Pi. You can use a terminal or a text editor with root privileges to access and modify this file.
  • Backup the original config.txt file: Before making any changes, it is always a good idea to create a backup of the original file. You can simply copy and paste the file to another location or rename the original file to something like "config.txt.backup".
  • Edit the config.txt file: Open the config.txt file in a text editor. Look for the line that says "hdmi_drive". You may need to uncomment this line by removing the "#" symbol at the beginning of the line. Change the value of hdmi_drive to "2" to force the output to HDMI. For example: hdmi_drive=2.
  • Save and reboot: Once you have made the changes, save the config.txt file and then reboot your Raspberry Pi for the changes to take effect.

Hdmi_drive=2

Lcd_rotate=2

Dtparam=audio=on

Gpu_mem_256=128

Gpu_mem_512=256

Gpu_mem_1024=256

Overscan_scale=1

Please note that you may need to adjust other settings in the config.txt file to match your specific hardware configuration. Also, keep in mind that changes to config.txt will only take effect after a reboot.

Additionally, if you want to switch between an LCD display and an HDMI monitor dynamically, you can create multiple config files (e.g., config_lcd.txt and config_hdmi.txt) and use a shell script to copy the correct config file based on certain conditions. For example, you can check if an HDMI cable is connected and then copy the appropriate config file and reboot the Pi. This way, you can toggle between the LCD and HDMI simply by unplugging and rebooting.

shundigital

Use the tvservice command to change the display

The tvservice command can be used to change the display on a Raspberry Pi. The command is available on Raspbian and may be available on other operating systems.

To switch to an HDMI display, use the following command:

Tvservice -n

To switch back to the LCD display, use the following command:

Tvservice -o

Note that these commands will only work if the Raspberry Pi has detected the external HDMI monitor. This can be done by disconnecting the HDMI cable and rebooting the Raspberry Pi.

It is also important to note that the tvservice command may not work in all cases. In some cases, the vcgencmd command may be a better option for turning the display on and off. The following commands can be used to turn the display on and off using vcgencmd:

Vcgencmd display_power 0

To turn the display off, and

Vcgencmd display_power 1

To turn the display back on.

shundigital

Change the resolution to match the LCD

Changing the resolution of your Raspberry Pi to match your LCD monitor can be done through the desktop interface or the terminal.

Changing the Resolution Using the Desktop Interface

  • Click the Raspberry Pi icon in the top-left corner of the screen to bring up the start menu.
  • Hover over "Preferences" and click "Screen Configuration" or "Screen Configuration Tool".
  • Right-click the display that you want to modify the resolution of.
  • Hover over "Resolution" and select your desired resolution from the list of options.
  • Click "Apply" to apply the changes.
  • Click "OK" within 10 seconds to confirm the changes.

Changing the Resolution Using the Terminal

Raspberry Pi OS Wayland

Get the device name and a list of supported screen resolutions by running the following command:

WAYLAND_DISPLAY="wayland-1" wlr-randr

  • Note the output name and the resolution you want to set.
  • Run the following command to set the screen resolution:

WAYLAND_DISPLAY="wayland-1" wlr-randr --output --mode x@Hz

Replace `` with the name of the output on which you want to set the screen resolution, `` and `` with your desired resolution, and `` with your desired refresh rate.

Raspberry Pi OS X11

Get information about your connected monitors by running the following command:

DISPLAY=:0 xrandr

  • Note the resolution, refresh rate, and output device name.
  • Run the following command to set the screen resolution:

DISPLAY=:0 xrandr --output --mode x --refresh

Replace `` with the output name for your display output, `` and `` with your desired resolution, and `` with your desired refresh rate.

Setting the Screen Resolution at Boot

Determine the output device name based on the connection you are using:

  • HDMI-A-1: HDMI 1 on the Raspberry Pi. Labelled as "HDMI 0" on the Raspberry Pi 4.
  • HDMI-A-2: HDMI 2 output on the Raspberry Pi 4. Labelled as "HDMI 1" on the board.
  • DSI-1: DSI or DPI connection on the Pi.
  • Composite-1: Composite connection on the Raspberry Pi.

Modify the "cmdline.txt" file by adding the following to the end of the line:

Video=:xM@

Replace ``, ``, ``, and `` with the appropriate values for your setup.

  • Save and exit the text editor.
  • Restart the Raspberry Pi for the changes to take effect.

Legacy Method for Modifying the Boot Configuration

Modify the "/boot/firmware/config.txt" file by running the following command:

Sudo nano /boot/firmware/config.txt

Determine whether to use the CEA or DMT HDMI groups:

  • CEA (Consumer Electronics Association): Typically used on TVs and represented by the number 1.
  • DMT (Display Monitor Timings): Typically used by monitors and represented by the number 2.
  • Set the hdmi_mode variable in the config file based on your chosen HDMI group and desired resolution. Refer to the official Raspberry Pi Video documentation for a list of supported resolutions.
  • Save and exit the text editor.
  • Restart the Raspberry Pi for the changes to take effect.

shundigital

Use the Raspberry Pi Configuration Tool to set the resolution

The Raspberry Pi Configuration Tool makes it easy to set the resolution on your Raspberry Pi. The first step is to access the tool. To do this, click on the Raspberry Pi icon in the top-left corner of the screen to open the main menu. From there, scroll to "Preferences" and find the "Screen Configuration" tool.

Once you have the "Screen Configuration" tool open, you will see the current configuration. If you have two monitors connected, you will see two squares (HDMI-1 and HDMI-2). You can move these squares around if you need to revert the left and right displays.

Next, right-click on the square representing the monitor you want to configure. This will bring up a list of options that you can use to change the default values. Pick the resolution you want to try, then click "Apply".

A confirmation message will appear to ensure that the new resolution works. Click "OK" if it looks fine. If it doesn't work or you lose the display completely, the configuration will revert to the previous version after 10 seconds. So, it's safe to test various resolutions, even if your monitor doesn't support them.

In addition to adjusting the screen resolution, you can also use the "Screen Configuration" tool to change the frequency or orientation of your display.

If, for any reason, the configuration with the right-click is not comfortable on your setup, you can use the "Layout" menu. The same options are available under "Layout" > "Screens".

Setting the headless resolution for VNC connections

The Raspberry Pi Configuration Tool also allows you to set the headless resolution for VNC connections. This will determine the default resolution that users will have when they use VNC to remotely log in to the Raspberry Pi.

To set the headless resolution, open the Raspberry Pi Configuration Tool and select "Display Options". Then, highlight the option for VNC resolution and press Enter. Finally, select your desired resolution from the menu.

Using the terminal to set the resolution

While the Raspberry Pi Configuration Tool provides a graphical way to set the resolution, you can also use the terminal to achieve the same result. The specific steps depend on whether your Raspberry Pi is running Wayland or X11.

To determine which one your Raspberry Pi is using, simply check the version of Raspberry Pi OS you are running. Older versions of Raspberry Pi OS and Pi's older than the Pi 4 used X11, while newer versions and the Pi 4 and Pi 5 use Wayland by default.

Setting the resolution on Wayland

To set the screen resolution on Wayland, you will need to know the device name and a list of supported screen resolutions. You can get this information by running the following command in the terminal:

WAYLAND_DISPLAY="wayland-1" wlr-randr

This command sets the "WAYLAND_DISPLAY" environment variable to "wayland-1", allowing you to interact with Wayland from the terminal.

The output of the above command will include the screen resolution your monitor can be set to and the refresh rate. Take note of the output device name for your monitor, which will be something like "HDMI-A-1" or "HDMI-A-2".

Once you have the output name and the desired resolution, you can set the screen resolution with the following command:

WAYLAND_DISPLAY="wayland-1" wlr-randr --output --mode x@Hz

Replace `` with the output name, `` and `` with the desired resolution, and `` with the refresh rate.

For example, to change the resolution to 1920x1080 at 60Hz, you would use the following command:

WAYLAND_DISPLAY="wayland-1" wlr-randr --output HDMI-A-2 --mode 1920x1080@60Hz

Setting the resolution on X11

To set the screen resolution on X11, you can use the `xrandr` tool. First, run the following command to get information about your connected monitors:

DISPLAY=:0 xrandr

Here, we are setting "DISPLAY=:0" to talk with display "0".

The output of the above command will include the resolution, refresh rate, and output device name. Take note of these values.

Once you have the necessary information, you can set the screen resolution with the following command:

DISPLAY=:0 xrandr --output --mode x --refresh

Replace `` with the output name, `` and `` with the desired resolution, and `` with the refresh rate.

For example, to set the resolution of a screen connected to HDMI-2 to 1920x1080 with a refresh rate of 60Hz, you would use the following command:

DISPLAY=:0 xrandr --output HDMI-2 --mode 1920x1080 --refresh 60

Setting the screen resolution at boot

You can also set the screen resolution of your Raspberry Pi at boot by modifying the "cmdline.txt" file. To do this, open the file with a text editor (e.g. `sudo nano /boot/firmware/cmdline.txt`) and add the following to the end of the line:

Video=:xM@

Replace `` with the output device name, `` and `` with the desired resolution, and `` with the refresh rate.

For example, to set the resolution of the second HDMI connection (HDMI-A-2) to 1920x1080 at 60Hz, you would add the following to the end of the line:

Video=HDMI-A-2:1920x1080M@60

After making the change, save and exit the text editor. If your Pi is already running, you will need to restart the device for the change to take effect.

Legacy method for modifying the boot configuration

If you are running an older version of Raspberry Pi OS, you can set the screen resolution by modifying the `config.txt` file located in the `/boot/firmware/` folder. Open the file with a text editor (e.g. `sudo nano /boot/firmware/config.txt`) and follow the steps below:

  • Determine whether you want to use the CEA or DMT HDMI groups. CEA (Consumer Electronics Association) is typically used on TVs and is represented by the number 1. DMT (Display Monitor Timings) is typically used by monitors and is represented by the number 2.
  • Choose the resolution you want to use by finding the corresponding `hdmi_mode` value in the official Raspberry Pi Video documentation.
  • Set the resolution by adding the following line to the `config.txt` file, replacing `[YOURHDMIMODE]` with the `hdmi_mode` value you found in the previous step:

Hdmi_mode=[YOURHDMIMODE]

Frequently asked questions

You can switch between an LCD monitor and an HDMI monitor by editing the config.txt file. The exact steps to do this will depend on your specific setup, but generally, you will need to disconnect the HDMI cable and reboot your system to switch to the LCD monitor.

To switch back to an HDMI monitor, you will need to connect the HDMI cable and reboot your system.

To change the screen resolution of your Raspberry Pi, you can use the raspi-config tool. This tool allows you to adjust various settings, including the screen resolution. You can also manually edit the config.txt file to change the resolution by adjusting the framebuffer width and height values.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment