Wireless nanosensor networks (WNSNs), which consist of nanosensors with the size of just a few hundred nanometers, can sense new types of events at nanoscale and thus are promising for lots of important applications, like intrabody drug delivery, air pollution surveillance, etc. Since nanosensors are highly energy-constrained, it is critical to adopt energy-efficient protocols for communications in WNSNs. This paper focuses on WNSNs adopting the on–off keying modulator which transmits a signal for a high bit and keeps silence for a low bit and explores the optimal coding design for transmission energy minimization in such networks. Specifically, we focus on the design of low-weight code which maps m-bit symbols into n-bit codewords with fewer high bits. First, for given symbol length m and codeword length n, we determine the optimal codebook with the least average number of high bits per codeword and give the corresponding average codeword weight as a function of m and n. Then, we study the minimum transmission energy (MTE) code design problem with codeword length and codeword rate constraints and investigate how the transmission energy changes when m and n are varied. Finally, the corresponding optimal settings of m and n are determined for the MTE code design problem to achieve the minimum transmission energy consumption per data bit.