In data transmission in a wireless sensor network (WSN), a lot of energy will be consumed, but the energy of WSN is limited. The energy consumption of WSN is optimized to ensure the performance of WSN. This paper first introduced WSN and routing protocol, took Low Energy Adaptive Clustering Hierarchy (LEACH) protocol as the subject, analyzed its workflow and problems, and optimized it with the particle swarm optimization (PSO) algorithm to obtain the PSO-LEACH protocol. The experimental results demonstrated that the Fist Node Die (FND) of the PSO-LEACH protocol appeared in the 946th round and Half Node Die (HND) appeared in the 971st round, both slower than the LEACH protocol, indicating that the PSO-LEACH protocol had a longer life cycle; in the same number of rounds, the energy consumption of the PSO-LEACH protocol was always lower than the LEACH protocol, and the time when all energy was consumed was later than the LEACH protocol; when the energy consumption was the same, the PSO-LEACH protocol received more data than the LEACH protocol. The experimental results show that the improved LEACH protocol can optimize the energy consumption of WSN, which is conducive to energy saving and high efficiency of WSN and can be popularized and applied in practice.