In order to explore new ways to detect and remove heavy metal ions from industrial wastewater, the first-principles method based on density functional theory has been used to investigate the performance of carbon nanotubes (CNTs) in adsorbing divalent heavy metal ions which include Zn2+, Cu2+, Pb2+ and Sn2+. Results show that the adsorption of Zn2+ on CNTs is weak and only physical adsorption forms between them. However, for Cu2+, Pb2+ and Sn2+, the final adsorption distance with CNTs is greatly decreased, and the adsorption energy and charge transfer amount with CNTs are significantly increased. In addition, the charge density of Cu2+, Pb2+ and Sn2+ overlaps effectively with that of CNTs. These indicate the formation of strong chemisorption between these ions and CNTs. Therefore, CNTs could be used as a sensing material to detect and remove Cu2+, Pb2+ and Sn2+ from wastewater. The research provides theoretical guidance for the application of CNTs in heavy metal ions treatment.