In wireless sensor networks each sensor node has very limited resources, and it is very difficult to find and collect them. For this reason, updating or adding programs in sensor nodes must be performed via a communication channel at run-time. Many code update protocols have been developed for sensor networks, ranging from function-level update to full-image replacement. However, they provide only a fixed level of code update protocols. These protocols require manual selection of an appropriate protocol because they do not consider a cost analysis of the update protocols. In addition, they do not consider real-time response while updating codes. In this paper, we present an adaptive multilevel code update protocol (AMCUP) for real-time sensor operating systems. AMCUP enables energy-efficient code update via support for multilevel protocols (i.e., full-image, module-level, function-level, and instruction-level). It adaptively selects a protocol which meets deadline of applications and c...