Typical Chinese herbal gallnut residue, an organic waste threatening the environment during the modernization of traditional Chinese medicine, was used as a precursor to prepare heteroatom co-doped hierarchical porous carbon materials with electrochemical properties.