In recent years, neural networks have achieved phenomenal success across a wide range of applications. They have also proven useful for solving differential equations. The focus of this work is on the Poisson–Boltzmann equation (PBE) that governs the electrostatics of a metal–oxide–semiconductor capacitor. We were motivated by the question of whether a neural network can effectively learn the solution of PBE using the methodology pioneered by Lagaris et al. [IEEE Trans. Neural Netw. 9 (1998)]. In this method, a neural network is used to generate a set of trial solutions that adhere to the boundary conditions, which are then optimized using the governing equation. However, the challenge with this method is the lack of a generic procedure for creating trial solutions for intricate boundary conditions. We introduce a novel method for generating trial solutions that adhere to the Robin and Dirichlet boundary conditions associated with the PBE. Remarkably, by optimizing the network parameters, we can learn an optimal trial solution that accurately captures essential physical insights, such as the depletion width, the threshold voltage, and the inversion charge. Furthermore, we show that our functional solution can extend beyond the sampling domain.