Nowadays, transmitting and receiving data with high speed and a high level of security are the main demands. So, a new model of spectral amplitude coding optical code division multiple access (SAC-OCDMA) is suggested in this paper, based on a free space optical (FSO) communication system using coherent sources. Three different codes: enhanced double weight (EDW), modified double weight (MDW) and multi-diagonal (MD) codes are assigned to our proposed model with the direct detection (DD) technique. Furthermore, the model is simulated under different weather conditions including clear air (CA), light mist (LM), very light fog, and light fog (LF). The system performance is evaluated through its bit error rate (BER), Q-factor, received power, and signal to noise ratio (SNR). Moreover, classification of the information received by the three different SAC-OCDMA using three different codes (EDW, MDW, and MD) is still challenging. So, two different machine learning (ML) algorithms are used, in this study, to classify the data received using the different codes. Detecting which code is received at the receiver end is important in order to reduce code error detection. Two algorithms: K-Nearest Neighbor (KNN) and Support Vector Machine (SVM) are adopted to classify different codes used for data transmission under four different weather conditions. The ML input dataset consists of the obtained simulation results, including Q-factor, BER, and SNR. Each feature is to be normalized before using ML. The obtained results show that the performance of the proposed FSO model achieved the longest propagation range under CA weather conditions, 7 km, while the shortest is under LF, which is 0.98 km. This is due to the attenuation of fog that causes signal degradation. The FSO system that uses EDW gives the best result under different weather conditions, while the system that uses MD code gives the worst performance. Also, the highest power is achieved when the EDW code is used at 5.5 km. The EDW has a received power of − 21.58 dBm, while the received power is − 22.04 dBm and − 23.8 dBm for MDW and MD codes, respectively. As for classification between the received information coming from three different codes under different weather conditions, both algorithms, KNN and SVM, achieve promising results in most cases. They showed more than 97% of classification accuracy under CA, LM, and LF weather conditions.