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 |
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.
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.
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.
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