# Slotted ALOHA on LoRaWAN-Design, Analysis, and Deployment
## 5 Real Time Clock Synchronization over LoRaWAN Stack  
Clock synchronization across end nodes is fundamental to define the slots used by S-ALOHA. In literature, articles experimented some synchronization schemes on the top of the bare LoRa radio [28], without any LoRaWAN protocol to demonstrate that LoRa modulation can be used even industrial environments [29]. Here we use a lightweight synchronization methodology, presented in Ref. [27] and tailored for LoRaWAN end nodes and successfully tested on an STM32L4 MCU. To generate a time reference for the S-ALOHA protocol, where the slots must be kept aligned in all the end devices, we use a real-time clock (RTC) with an inexpensive external crystal at 32.768 KHz with a thermal variability between 20 to 80 ppm/\({}^{\circ}\)C. Since this component drifts significantly with temperature, we need to re-synchronize end node clocks frequently to keep slots sufficiently aligned. The synchronization scheme is piggybacked on the predefined answer (ACK) of a LoRaWAN Class A RX1 window, which is opened with a maximum error of \(\pm\)20 \(\upmu\)S delay [27]. Common events for both devices, end node and gateway, are used as a shared reference for the synchronization algorithm. In a typical half-duplex link, the node and the gateway must know when to open the RX1 window after a successfully uplink transmission. A timestamp at the end of such packet is saved on both devices with the goal to open the RX1 window simultaneously; thus, this event (Figure 7) is used as the reference for our clock synchronization procedure [27]. The synchronization algorithm is based on a clock reference distribution system: basically, at the end of every complete uplink transmission each node updates its real-time clock. We implemented an algorithm that works as follows: (1) gateway and node both save a timestamp of the moment when the uplink transmission has ended; (2) the node receives, in the first available RX window, gateway's timestamp piggybacked in the acknowledgment message; (3) due to the non-negligible flight times and the RX window's delay, the node must consider the offset between the timestamp when the uplink transmission has ended (TX timestamp) and the one when it receives the acknowledgement message (RX timestamp). Node calculates the difference between these two timestamps and adds it to the timestamp received by the gateway. (4) The node updates its real-time clock with the new timestamp, in this way both devices are synchronized. The developed system must automatically update the end node RTC to guarantee an adequate alignment with the gateway's reference clock. Moreover, the protocol must be compliant with LoRaWAN specification without adding a significant overhead on the overall communications. The proposed approach uses the LoRaWAN uplink packet and the RX window already scheduled after 1 s; indeed, if the timestamp saved on gateway, received at the end of the uplink transmission, can be sent back with the ACK packet in the RX1, the end node has the time reference needed for a clock synchronization. Overhead is very low, only 8 bytes of timestamp, with good scalability and configurability [27]. In many applications the overhead is negligible, for example when the end node  
Figure 7: Synchronization procedure. generates many data packets, the synchronization comes for free adding the gateway timestamp at some downlink packet. In WSN where the end node is programmed to send few packets every day, some additional synchronization phases will be needed to ensure clocks do not drift too much. Maximum tolerable clock drift depends on three factors: (i) maximum uplink time of air; (ii) daily duty cycle of each end node; (iii) maximum throughput required for the specific application. On the end node side, the purpose of the RTC synchronization function, implemented in firmware, is to update the time contained in the local RTC with the timestamp received from the gateway, this procedure requires two steps: the calculation of the offset and the management of the sub-seconds.