5V to 3.3V Logic Level Translator using a 74HC4050 - 2025.06.07
Many microcontrollers run with 5V logic, like the Arduino Uno and the ATMEGA 2560, but these days most chips and modules use 3.3V. Unless the 3.3V device has '5V tolerant' inputs, you cannot connect a 5V device to these 3.3V inputs because they can be damaged. To connect these together you need a logic level translator to drop the signal's voltage from 5V to 3.3V.
Sometimes you may have a digital or on/off signal which is up to 15V and you want to read this safely with your 5V or 3.3V controller. A logic level translator can be used here too.
When going the other way round, connecting a 3.3V output to a 5V input, you don't usually need a logic level translator because the 5V input will see a '1' from a 3.3V logic level.
A chip that's commonly used for this is the 74HC4050 Hex non-inverting high-to-low level shifter. This converts up to 6 inputs from a higher voltage to a lower voltage. For example, 5V->3.3V, or 12V->5V, or 9V->3.3V etc. To use this chip, connect the VDD pin (1) to the lower voltage of the device to receive the inputs, e.g. to 3.3V.
The inputs are Schmitt triggers, making the switching very fast and less prone to noisy signals.
The 74HC4050 chip is no longer manufactured in a DIP-16 package (0.1inch/2.54mm pins), but you can still buy them online until stocks run out - on Ricardo.ch, ebay and Amazon. Experimenters and developers prefer the old DIP package because it's easy to use them with a breadboard. Whereas the tiny SOIC-16, SSOP-16 and TSSOP-16 versions must first be soldered onto an adapter board, which is difficult to do.
There are other ways to do logic level translation, even using a basic resistor divider, but these affect the signal and may not work for the high frequencies needed by SPI communications.
This is the circuit of a 3.3V SD Card Reader which uses this chip to interface it to a 5V microcontroller. The 74HC4050 chip has VDD connected to 3.3V.
The MISO output signal does not need conversion. The same circuit can be used to connect any 3.3V SPI device to a 5V controller.
This circuit is used in many 3D printer boards, like this Creality 3D V2.5.2 motherboard.
(I have reverse-engineered this undocumented Creality board and written new C++ code for it to control a coil winding machine.)
For those wearing anoraks, here's the include file that defines the pins on this board: ⬛Pins_Creality_3D_V2_5_2.h [Click to expand]
#pragma once
// Creality 3D V2.5.2 Board from old Creality CR-10 V3 printer
// ATMEGA 2560 Microcontroller
// Copyright (C) muman.ch, 2025.04.04
// All rights reversed
/*
NOTE: This board needs a 24V supply (not 12V like many other boards)!
Where possible, the same pin names used by standard 3D printer firmware are used.
Marlin is a typical implementation:
https://github.com/MarlinFirmware/Marlin
See also RAMPS (RepRap Arduino Mega Pololu Shield). The Creality boards and
software are based on the old RAMPS hardware design:
https://reprap.org/wiki/RAMPS_1.4
ATMEGA 2560 DATA SHEET
https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/ATmega640-1280-1281-2560-2561-Datasheet-DS40002211A.pdf
SUMMARY OF THE BOARD'S I/Os
---------------------------
MAIN BOARD
Serial USB, RXDO/TXD0
Limit switch inputs, active low with 10K pull-ups, X+/Y+/Z+/X-/Y-/Z-
(Filament runout sensor (microswitch) was connected to X+ limit input)
TMC2208 stepper motor controllers for 4 motors, X/Y/Z and E1 (extruder)
MOSFET fan control output 24V PWM, K-FAN1/K-FAN2
MOSFET heater control 24V PWM outputs for 3 heaters, BED/HEAT1/HEAT2
Analogue NTC/Pt temperature sensor inputs x 3, with 4K7 pull-ups, TH1/TH2/TB
Onboard LED output (fan and heater outputs also have LEDs)
SD Card reader using hardware SPI
J1 6-pin ICSP connector for program download using Atmel-ICE
12-pin AUX connector, marked SGV (Signal/GND/VCC), 2 digital in/out/PWM + 2 analogue inputs
6-pin white Serial2 connector (RX2/TX2 pins are also used connected to EXP1)
12864ZW-10 LCD DISPLAY PANEL via EXP1 and EXP2 connectors
Rotary encoder signals EN1/EN2 + push-button ENC
Beeper output
128x64 LCD graphical display using software SPI
NOTES
-----
ATMEL ICE (AVR) via J1 ICSP CONNECTOR
There is no bootloader, the Atmel-ICE must be used to download a program.
https://www.microchip.com/en-us/development-tool/atatmel-ice
https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/UserGuides/Atmel-ICE_UserGuide.pdf
The Creality board has a 6-pin ICSP connector (J1) for connecting the Atmel-ICE.
ICSP does not work for debugging, so debugging must be done by outputting
debug and error messages on 'Serial', see Utils.h.
>>> YOU CANNOT USE THE SD CARD AND THE ICSP CONNECTOR AT THE SAME TIME <<<
>>> They both use the hardware SPI <<<
USB CHIP IS CH340G
https://cdn.sparkfun.com/datasheets/Dev/Arduino/Other/CH340DS1.PDF
The CH340G chip is connected to RXD0/TXD0, so the standard 'Serial' is
the serial USB.
USB is used for communications with the PC application, it is not used
for programming. For that you must use the Atmel-ICE connected to ICSP.
There is a blue LED on RXD0 which flashes when data is received.
The DTR output of the CH340 was connected via a 100nF capacitor to the
microcontroller's RESET pin. This allowed it to be reset whenever a
sketch was uploaded. This is very annoying if you're using the USB for
communications. I have removed this capacitor because the Atmel-ICE does
the reset itself via the ICSP interface.
LIMIT SWITCHES
Active low, with 10K pullup.
The MEGA 2560 interrupt pins are: 2, 3, 18, 19, 20, 21
Only the X and Z axis limit switches can generate interrupts via X_MIN_PIN,
X_MAX_PIN, Z_MIN_PIN, Z_MAX_PIN.
If microswitches are used, they MUST be debounced, see 'DebouncedSwitch.h'.
The best limit switch is a 5V infrared (IR) speed or limit switch module
which may not need debouncing if it has a built-in comparator with
hysteresis (LM393 etc).
The filament runout sensor microswitch was connected to the X+ limit
switch input.
TEMPERATURE SENSORS
These can be resistive NTC or Pt sensors, 10K or 100K. A 4K7 pull-up
resistor is fitted. See 'NTCThermistor.h' and 'PT100x.h'.
SD CARD
This uses the hardware SPI.
I used the Arduino SD Card library, with a wrapper in 'SDCard.h'
https://docs.arduino.cc/libraries/sd/
>>> YOU CANNOT USE THE SD CARD AND THE ICSP CONNECTOR AT THE SAME TIME <<<
>>> They both use the hardware SPI <<<
STEPPER MOTOR CONTROL
Four nice silent TMC2208 controller chips are used.
The AccelStepper library controls the motors:
https://www.airspayce.com/mikem/arduino/AccelStepper/
https://www.pjrc.com/teensy/td_libs_AccelStepper.html
https://github.com/swissbyte/AccelStepper
LCD DISPLAY PANEL 12864ZW-10
The Software SPI is used because the hardware SPI is for the SD Card reader
and ICSP on the main board.
The U8G2 library runs the display:
https://github.com/olikraus/u8g2
This Arduino menu library will be used to run the menus:
"GEM Good Enough Menu" - seems to be the best...
https://github.com/Spirik/GEM?tab=readme-ov-file
https://forum.arduino.cc/t/gem-good-enough-menu-library/501115
ROTARY ENCODER and PUSH BUTTON on 12864ZW-10 DISPLAY PANEL
See 'RotaryEncoder_12864ZW10.h'
5V SUPPLY FROM 24V
Uses this buck converter chip
https://www.sg-micro.com/rect/assets/9fe04e94-334c-4982-964b-fc08001cd9ac/SGM6232.pdf
DC motor control with PWM
https://www.portescap.com/-/media/project/automation-specialty/portescap/portescap/pdf/whitepapers/wp_controlling_brushed_DC_motors_using_pwm.pdf
https://electronics.stackexchange.com/questions/242293/is-there-an-ideal-pwm-frequency-for-dc-brush-motors
*/
// It's an 8-bit CPU, with 16-bit ints and no FPU
#define CPU_8_BIT
// To convert the pin number to a microcontroller pin
// see 'digital_pin_to_port_PGM[]' in this file:
#include <pins_arduino.h>
// LIMIT SWITCH INPUTS
// For microswitches or powered IR photosensors.
// The *_MAX_PINs have 3 pins with a +5V supply for a powered sensor (IR).
// Microswitches MUST be debounced, see 'DebouncedSwitch.h'.
// IR photosensor modules may not need debouncing if they use a comparator
// chip with hysteresis.
// X and Z switches can generate interrupts for emergency stop.
// X-/Y-/Z- have 2-pin connectors.
// X+/Y+/Z+ have 3-pin connectors with 5V pin for powered IR photsensors.
// Z- (Z_MIN_PIN) could be used as a Z-Probe by RAMPs firmware.
// X+ (X_MAX_PIN) was used for the filament runout sensor microswitch.
//
// All have 10K pull-up to 5V and 10K series resistor to input, no capacitor:
//
// 5V----|10K|---+----o SW o----GND
// |
// +----|10K|---->PIN input
#define X_MIN_PIN 3 // X- PE 5 ** 3 ** PWM3 interrupt
#define X_MAX_PIN 2 // X+ PE 4 ** 2 ** PWM2 interrupt Filament runout sensor
#define Y_MIN_PIN 14 // Y- PJ 1 ** 14 ** USART3_TX
#define Y_MAX_PIN 15 // Y+ PJ 0 ** 15 ** USART3_RX
#define Z_MIN_PIN 18 // Z- PD 3 ** 18 ** USART1_TX interrupt
#define Z_MAX_PIN 19 // Z+ PD 2 ** 19 ** USART1_RX interrput
// TMC2208 STEPPER MOTOR DRIVER PINS
// https://www.analog.com/media/en/technical-documentation/data-sheets/TMC2202_TMC2208_TMC2224_datasheet_rev1.13.pdf
// https://github.com/teemuatlut/TMC2208Stepper
// NOTE! The driver *_ENABLE_PINs are ACTIVE LOW! HIGH=Motor power OFF.
// MS1 and MS2 both tied to 5V, so it's hard-wired for 16 microsteps.
// PDN/UART is not used, has pull-up to 5V.
// The DIAG output is not connected.
#define X_STEP_PIN 54 // PF 0 ** 54 ** A0
#define X_DIR_PIN 55 // PF 1 ** 55 ** A1
#define X_ENABLE_PIN 38 // PD 7 ** 38 ** D38 Active low
#define Y_STEP_PIN 60 // PF 6 ** 60 ** A6
#define Y_DIR_PIN 61 // PF 7 ** 61 ** A7
#define Y_ENABLE_PIN 56 // PF 2 ** 56 ** A2 Active low
#define Z_STEP_PIN 46 // PL 3 ** 46 ** D46
#define Z_DIR_PIN 48 // PL 1 ** 48 ** D48
#define Z_ENABLE_PIN 62 // PK 0 ** 62 ** A8 Active low
#define E0_STEP_PIN 26 // PA 4 ** 26 ** D26
#define E0_DIR_PIN 28 // PA 6 ** 28 ** D28
#define E0_ENABLE_PIN 24 // PA 2 ** 24 ** D24 Active low
// E2 motor driver chip is not fitted
// >>>these three I/Os could be re-purposed<<<
//#define E2_STEP_PIN 36 // PC 1 ** 36 ** D36
//#define E2_DIR_PIN 34 // PC 3 ** 34 ** D34
//#define E2_ENABLE_PIN 30 // PC 7 ** 30 ** D30
// TEMPERATURE SENSORS
// 10-bit analog inputs, 0..1023.
// Use an RTD device, NTC 10K/100K or PT100/PT1000.
// See 'NTCThermistor.h' and 'PT100x.h'.
// Each has a 4.7K pull-up to 5V forming a voltage divider with the
// sensor, and a filter capacitor:
//
// 5V----|4K7|----+---o RTD o---+----GND
// | |
// | === 0.1uF
// | |
// +-------------+---->PIN analogue input
#define TEMP_0_PIN 67 // PK 5 ** 67 ** A13 TH1
#define TEMP_1_PIN 69 // PK 7 ** 69 ** A15 TH2
#define TEMP_BED_PIN 68 // PK 6 ** 68 ** A14 TB
// HEATERS AND FANS
// 24V PWM outputs via MOSFET switches.
// BED MOSFET tested up to 5kHz.
// These can also be used for PWM DC motor speed control, but be
// sure to ADD A DIODE in parallel with the motor (e.g. 1N4007)!
//
// +24V---+---o MOTOR o---+---< PWM output, switched to GND
// | |
// +------|<-------+ 1N4007
//
// Note: FAN1, FAN2, FAN3 are 24V uncontrollable fans, always on.
//
//#define MOSFET_Q1_PIN 9 // Q1 PH 6 ** 9 ** PWM9
//#define MOSFET_Q2_PIN 7 // Q2 PH 4 ** 7 ** PWM7
//#define MOSFET_Q3_PIN 8 // Q3 PH 5 ** 8 ** PWM8
//#define MOSFET_Q4_PIN 10 // Q4 PB 4 ** 10 ** PWM10
//
// The fan pin and each heater pin has an associated blue LED
// NOTE These pins cannot be read, digitalRead() always returns 1
#define FAN_PIN 9 // MOSFET Q1 K-FAN1 K-FAN2 Controllable fans
#define HEATER_0_PIN 10 // MOSFET Q4 HEAT1
#define HEATER_1_PIN 7 // MOSFET Q2 HEAT2
#define HEATER_BED_PIN 8 // MOSFET Q3 BED VS4604DM N-CH 40V 2mOhms 214W
//https://datasheet4u.com/pdf-down/V/S/4/VS4604DM-VanguardSemiconductor.pdf
// EXP1 AND EXP2 CONNECTORS for the 12864ZW-10 DISPLAY PANEL
//
// --------------------------------------------+
// +------- -------+ +------- -------+ | TOP VIEW
// | 2 4 6 8 VCC | | 2 4 6 8 10 | |
// | 1 3 5 7 GND | | 1 3 5 7 GND | |
// +-----------------+ +-----------------+ |
// EXP1 EXP2 |
// |
#define EXP1_01_PIN 37 // BEEPER_PIN // PC 0 ** 37 ** D37
#define EXP1_02_PIN 35 // BTN_ENC_EN // PC 2 ** 35 ** D35
#define EXP1_03_PIN 17 // PH 0 ** 17 ** USART2_RX RX2 same as aux connector, marked on underside [LCD_EN]
#define EXP1_04_PIN 16 // PH 1 ** 16 ** USART2_TX TX2 '' [LCD_RS]
#define EXP1_05_PIN 23 // PA 1 ** 23 ** D23 [LCD_D4]
#define EXP1_06_PIN 25 // PA 3 ** 25 ** D25 [LCD_D5]
#define EXP1_07_PIN 27 // PA 5 ** 27 ** D27 [LCD_D6]
#define EXP1_08_PIN 29 // PA 7 ** 29 ** D29 [LCD_D7
//#define EXP1_09_PIN 00 // GND
//#define EXP1_10_PIN 00 // VCC 5V
//#define EXP2_01_PIN 00 // nc (SPI_MISO not needed)
#define EXP2_02_PIN 52 // PB 1 ** 52 ** SPI_SCK
#define EXP2_03_PIN 31 // BTN_EN2 // PC 6 ** 31 ** D31
//#define EXP2_04_PIN 00 // nc [SD_SS sd card chip select]
#define EXP2_05_PIN 33 // BTN_EN1 // PC 4 ** 33 ** D33
#define EXP2_06_PIN 51 // PB 2 ** 51 ** SPI_MOSI
#define EXP2_07_PIN 49 // PL 0 ** 49 ** D49 [SD_DET]
//#define EXP2_08_PIN 00 // /RESET
//#define EXP2_09_PIN 00 // GND
#define EXP2_10_PIN 41 // PG 0 ** 41 ** D41 [KILL]
// ROTARY ENCODER on 12864ZW-10 Display Panel
#define BTN_ENC EXP1_02_PIN // PC 2 ** 35 ** D35 Push Button
#define BTN_EN1 EXP2_03_PIN // PC 6 ** 31 ** D31 Encoder 1
#define BTN_EN2 EXP2_05_PIN // PC 4 ** 33 ** D33 Encoder 2
// LCD DISPLAY on 12864ZW-10 Display Panel
// ST7920 controller
// Uses software SPI (the hardware SPI is used for the SD Card on the main board)
// Recommended: the u8g2 library https://github.com/olikraus/u8g2/
// For SPI mode the LCD_RS pin is used as the SPI chip select
#define ST7920_CS_PIN EXP1_04_PIN // LCD_RS TX2 // PH 1 ** 16 ** USART2_TX
#define ST7920_SCK_PIN EXP1_05_PIN // LCD_D4
#define ST7920_MOSI_PIN EXP1_03_PIN // LCD_EN RX2 // PH 0 ** 17 ** USART2_RX
// MISCELLANEOUS
#define BEEPER_PIN EXP1_01_PIN // beeper on 12864ZW-10 panel, see utils.cpp beep()
#define LED_PIN 13 // blue LED on main board
// Connected to DTR output of CH340G USB chip
#define SERIAL0_DTR 38 // PD 7 ** 38 ** D3
// ONBOARD SD CARD SLOT, uses hardware SPI
#define SD_CS_PIN 53 // PB 0 ** 53 ** SPI_SS, SPI Chip Select
#define SDSS SD_CS_PIN
//These definitions are not needed, the hardware SPI pins are already defined
//#define SD_MOSI_PIN 51 // PB 2 ** 51 ** SPI_MOSI
//#define SD_SCK_PIN 52 // PB 1 ** 52 ** SPI_SCK
//#define SD_MISO_PIN 50 // PB 3 ** 50 ** SPI_MISO
// 12-PIN AUX CONNECTOR, marked SGV
// S=Signal, G=GND; V=VCC 5V
// 2 digital inputs/outputs/PWM + 2 analogue inputs
// These can be used for anything (sometimes used to control fancy LEDs)
// They say the max. load is 20mA per output. I don't believe that. In
// practice I think it should be less than 5mA (min. 1K ohms).
// Analogue inputs have 100Meg ohms impedance.
// Input pull-ups are 30..50K ohms - a very weak pull-up.
#define SGV_D12 12 // D12 PB 6 ** 12 ** PWM12
#define SGV_D11 11 // D11 PB 5 ** 11 ** PWM11
#define SGV_A11 65 // A11 PK 3 ** 65 ** A11
#define SGV_A12 66 // A12 PK 4 ** 66 ** A12
// 6-PIN WHITE 'SERIAL2' CONNECTOR
// This can be used for 5V TTL serial communications if the LCD display
// panel is not connected - RX2 and TX2 are both used for the panel's
// software SPI interface.
// The pinout is printed on the back of the board:
// 1 GND, marked P1 on top of board
// 2 NC
// 3 RX2, connected to EXP1_03_PIN // PH 0 ** 17 ** USART2_RX
// 4 TX2, connected to EXP1_04_PIN // PH 1 ** 16 ** USART2_TX
// 5 NC
// 6 VCC 5V
This is a typical SD card interface for a 5V Arduino:
Here's a SOIC-16 version of the 74HC4050 chip soldered onto a 0.1" adapter board with a bypass capacitor, but these are hard to find.
This one has 5V marked on it - that's just the lower voltage - it can be 5V or 3.3V.
Using a MOSFET
Here's a really simple solution using any cheap N-channel MOSFET with a Vgs(th) of 2V or a bit lower, e.g. BSS123 (an N-channel logic level enhancement mode field effect transistor). This works in both directions, but you must ensure that the higher voltage (5V) is connected to the MOSFET's Drain, and the lower voltage (3.3V) to the Source. <never get your source and drain mixed up, especially if it's your water supply - ed>