1 Introduction

Semiconductor materials such as CdS, CdSe and CdTe are widely applied in many applications due to their optical characteristics (Yellaiah et al. 2014). CdS is member of II–VI chalcogenide materials that have a direct band gap of 2.42 eV and a small exciton Bohr radius of 3 nm (Giribabu et al. 1512). Nano CdS has excellent optical behaviors such as visible-light yield and appropriate conduction band potential for the photocatalytic applications (Pan et al. 2012). Furthermore, it can use in several applications such as solar cells, LED, flat panel displays, luminescent devices, labeling, imaging and sensor applications (Murphy 2002; Sakthivel et al. 2018). CdS can be formed in one or more phases (zinc blende or wurtzite or rock-salt) (Heiba et al. 2015a). The hexagonal and rock salt phases can be produced with ambient and high pressure conditions, respectively (Mueller et al. 2005). As the crystallite size of CdS reduced, a cubic structure can be obtained (Mueller et al. 2005). The phase transformation of CdS from one phase to another depends on many aspects for instance vacancies and interstitial of cadmium and stacking faults imperfections (Morales et al. 2001). As the crystallite size of CdS lies between 4–5 nm, CdS with both cubic and hexagonal phases can be obtained, only one phase can be found as the crystallite size of CdS is less or larger than 4–5 nm (Banerjee et al. 2000). First principle study revealed that (CdS)n (n = 1–8) cluster exhibited nonlinear optical responses due to the production of absorption peaks in the UV and visible ranges (Pegu et al. 2018). Also the thermodynamics characteristics of the (CdS)n cluster were increased as the temperature and the size of the cluster increased (Pegu et al. 2018). Density functional calculations showed that planar CdS sheet can be used in visible light photo-catalysis due to its lower band gap (Garg et al. 2016). In the photocatalyst applications of CdS, the production of recombination sites between photogenerated electrons is expected (Li et al. 2011, 2008). To solve this problem, suitable impurities can introduce inside CdS material, which can modulate its physical and chemical characteristics (Yücel and Beleli 2018). First principle study showed that the adaptable conducting character of the V or Cr-doped CdS monolayer nominated them to be used as a spin filter, create metal–semiconductor interface etc.… (Deb et al. 1953). On the other hand, the optical band gap of Mg-doped materials depends on the kind of the host materials, amount of Mg-doping, the dimension of the sample (1D, 2D or 3D) and the method of preparation. Devadoss et al. demonstrated that nano Cd0.9−xZn0.1MgxS material could choose for luminescence labels and imaging uses because its wide range of band gap, high optical transmittance and high photoluminescence emission (Devadoss and Sakthivel 2020). Yücel et al. found that the band gap of Mg-doped PbS thin film occurred within the solar spectral region (Yücel and Beleli 2018). Furthermore, Mg-doped PbS thin film (p-type semiconductor material) may be used as another buffer layer material for perovskite solar application (Yücel and Beleli 2018). The high optical transparency and low electrical resistivity values obtained in Mg-doped CdS films nominated them to be used as a potential candidate for a window material in solar cell uses (Sivaraman et al. 2014). Nanocomposite CdS/Mg–PVA has a notable improvement in the degradation of pollutants when irradiated with visible light (Krishnakumar et al. 2017). The optical energy gaps of CdS doped by 10%Mg prepared in air or underflows of nitrogen were increased in both cases as compared with undoped samples (Mohamed et al. 2018). The optical properties for nano ZnO have been changed significantly as it doped with Mg (Heiba et al. 2015b). The optical band gap of nano CdTe thin film increased (Kobyakov 2014), while it decreased in PbS or nano Cd0.9Zn0.1S or nano CdS samples as they doped with Mg (Giribabu et al. 1512; Yücel and Beleli 2018; Devadoss and Sakthivel 2020; Sivaraman et al. 2014). In this study, the reason for increasing or decreasing the band gap of CdS upon doping with 5%Mg is discussed using DFT calculation. The photoconductivity response of undoped and Mg-doped CdS (stoichiometric or non-stoichiometric) was also investigated. Therefore, CdS:5%Mg was prepared using the thermolysis technique in air and N2 atmospheres. The structural, microstructural, optical and electronic characteristics of the formed samples were explored in detail.

2 Methods and materials

CdS and Cd0.9Mg0.05S were synthesized using cadmium and magnesium acetates (Cd(CH3COO)2.2H2O, 98% and Mg(CH3COO)2·4H2O, 99%) and thiourea (SC(NH2)2, 99.98%) which were mixed simultaneously under two circumstances (air, N2 flow): The amount of cadmium acetate to thiourea was (1:1 and 1: 3) in both cases, respectively. The powders in both cases were mixed and then heated at 220 and 300 °C in air and under N2 flow in an electric oven, respectively (Mohamed et al. 2018). MAUD program relied on Rietveld profile procedure was applied on X-ray diffraction ((X’pert MPD, Philips, copper source) to find the crystal structure and microstructure of the different samples (Lutterotti 2010; Rodríguez-Carvajal 1993). Identification of all elements in each sample was examined by scanning electron microscope (FE-SEM, Quanta FEG-250). UV–vis and the photoluminescence spectra were performed using Shimadzu UV-3101PC UV–Vis–NIR and RF-1501 SHIMADZU, Ltd technique. Different electronic structure parameters were obtained applying Cambridge sequential total energy package (CASTEP) software (Perdew et al. 1996a) which relied on the first principle calculation. The supercell (211) of the cubic CdS phase, ultra-soft pseudopotentials and GGA-PBE condition were used during the calculation (Perdew and Wang 1992; Perdew et al. 1996b). The 4d105s2, 2p63p2 and 3s23p4, electrons related to Cd, Mg and S atoms were used as pseudopotential arrangements, respectively. The electronic properties of CdS and Mg-doped (CdS, Cd1−δS, CdS1−δ and CdS1−δOδ) samples were investigated.

The absorption coefficient, α(ω), and the refractive index of the material (n) are estimated from next relation using the real (ε1) and imaginary (ε2) parts of the dielectric function:

$$\alpha (\omega )=\sqrt{2}{\omega \left[\sqrt{{\varepsilon }_{1}^{2}+{\varepsilon }_{2}^{2}}-{\varepsilon }_{1}\right]}^{0.5}$$
(1)
$$n\left(\omega \right)={\left[\frac{{\varepsilon }_{1}(\omega )}{2}+\frac{\sqrt{{\varepsilon }_{1}^{2}(\omega )+{\varepsilon }_{2}^{2}(\omega )}}{2}\right]}^{0.5}$$
(2)
$${\varepsilon }_{1}\left(\omega \right)=1+\frac{2}{\pi }{\int }_{0}^{\infty }\frac{{\omega }^{.}}{{\omega }^{.2}-{\omega }^{2}}d{\omega }^{.}$$
(3)
$${\varepsilon }_{2}\left(\omega \right)=\frac{2{e}^{2}\pi }{\Omega {\varepsilon }_{o}}\left|\langle {\psi }_{k }^{c}\left|{\varvec{u}}\right.\times {\varvec{r}}\left|{\psi }_{k }^{\upsilon }\right.\rangle \right|\delta ({E}_{k}^{c}-{E}_{k}^{\upsilon }-E)$$
(4)

where ω is the angular frequency, u is vector represented the polarization of the incident electric field, r is the position operator, \({\Psi }_{k}^{c} \,\mathrm{and }{\Psi }_{k}^{v}\) are the probable transitions between valence and conduction bands. ε0, e and Ω are the dielectric constant in free space, charge of electron and volume of the unit cell.

The correlation between extinction coefficient (k) and the absorption of light can be estimated as the following:

$$k=\frac{\alpha \lambda }{4\pi }$$
(5)

The optical conductivity can be estimated using the dielectric constant ε(ω) as the following:

$$\sigma \left(\omega \right)=-i\frac{\omega }{4\pi }\left|\varepsilon (\omega )-1\right|$$
(6)

3 Results and discussion

3.1 X-ray diffraction and morphological investigations

Figure 1 illustrates the x-ray diffraction patterns fitting obtained from Rietveld refinement for the nano powder Cd0.95Mg0.05S prepared in the open air and under nitrogen. Although the patterns look like a single hexagonal phase, two phases were identified as cubic zinc blende and hexagonal wurtzite phases. Moreover, one can notice from the diffraction patterns that the samples prepared under nitrogen gas are much better in crystallinity than that prepared in air. No other phases were detected implying the incorporation of Mg ions substitutionally for Cd in the CdS lattice, a result was confirmed by Rietveld analysis. Moreover, Fig. 2 depicts the elemental analysis obtained for the samples which emphasize the incorporation of Mg into the CdS lattice with elements percentages intended in preparation. Table 1 reveals the refined structural parameters got using Rietveld refinement procedure. The cell parameters and z-coordinate of S ions were reduced for both phases as it doped with Mg ion owing to its smaller radius with respect to Cd ion. Furthermore, the cell parameters for the samples prepared under nitrogen flow are smaller than those for samples obtained in air, which may be ascribed to lesser defects present in samples in the case of N2 gas atmosphere. This claim is confirmed by the much bigger crystallite size for samples formed under nitrogen as shown in Table 1. Inspecting Table 1, one can notice that the phase percentages of cubic and hexagonal phases are almost equal in the case of samples formed in air, while for samples synthesized under N2 the hexagonal phase is dominant. For pure CdS prepared in N2, the sample has almost hexagonal structure with a tiny part of the cubic phase.

Fig. 1
figure 1

Rietveld refinement for CdS and Mg-doped CdS formed in a air and b N2 flow

Fig. 2
figure 2

EDS spectra for Mg-doped CdS formed in a air and b N2 flow

Table 1 Lattice parameters (a, c), crystallite size, z coordinate, and percentage of different phases (%) for CdS and Cd0.95Mg0.05S samples prepared in air and N2 circumstances

3.2 UV absorption study

The direct optical band gap (Eg) for Mg-doped CdS synthesis in air or under nitrogen atmosphere was evaluated from UV–Vis absorption measurements utilizing the Tauc’s formula (Tauc. 1972):

$$ \upalpha {\text{h}}\upnu = {\text{A}}\left( {{\text{h}}\upnu - {\text{E}}_{{\text{g}}} } \right)^{0.5} $$
(7)
$$ \upalpha = {2}.{3}0{\text{3xA}}/{\text{t}} $$

where α, and A are the absorption coefficient, the incident photon energy and a constant, respectively.

By plotting a relation between (αhυ)2 and , the direct optical band gap can be calculated by extending the linear part of (αhυ)2 to 0 with x-axis as demonstrated in Fig. 3. The obtained energy gaps are 1.95 and 2.35 eV for 5%Mg doping samples formed in air and N2, respectively, relative to undoped samples, 2.58 eV (CdS, air) and 2.12 eV (CdS, N2) (Mohamed et al. 2018), respectively. As revealed from the result, the optical band gap was decreased as the undoped sample prepared in N2 relative to the undoped one prepared in air. The reason for the reduction in the optical band gap of CdS (N2) sample as compared with CdS (air) sample is owing to the increasing of particle size of the sample formed using N2 flow relative to the sample formed in air (Wang et al. 2008) as listed in Table1. On the other hand, the optical band gap decreased as CdS (air) doped with Mg, while the optical band gap of CdS (N2) was increased as it doped with Mg. Previously, it was found that the optical band gaps of CdS doped with 10%Mg (air or N2 atmosphere) were enhanced as it doped with 10%Mg (Mohamed et al. 2018). The optical band gaps of CdS, Ga2O3 thin film, ZnO and TiO2 were enhanced as they doped with Mg (Sivaraman et al. 2016; Bi et al. 2018; Manju et al. 2018; Zeng et al. 2016). On the other hand, the energy gaps of CdSe and TiO2 film were reduced as they doped with Mg (Murugan et al. 2001; Joo et al. 2003). Furthermore, the optical band gap of ZnS was reduced from 3.94 to 3.72 eV then it increased again to 3.86 eV as the concentration of Mg doping raised in the matrix (Ashokkumar and Boopathyraja 2018). The increase in energy gap may be explained by Burstein–Moss (BM) effect (Sivaraman et al. 2016). Magnesium ion has less electro-negativity as compared with Cd ion. Consequently, the Mg ions shared further electrons than the Cd ions in the conduction band which caused a shift in the Fermi level thus raised the optical band gap energy of the doped sample (Sivaraman et al. 2016). Furthermore, the red and blue shifts in the energy gap may be owing to the size effect dependent, the percentage of cubic phase to the hexagonal phase of CdS in the sample and the existence of some oxygen in the sample due to the preparation of some samples in the air (Ashokkumar and Boopathyraja 2018). Further investigation of the reason of the increasing or decreasing in the value of the optical band gap as Mg-doped CdS in air or under N2 flow will be explored in the electronic section.

Fig. 3
figure 3

Relation between (ahυ)2 and hυ of Mg-doped CdS formed in a air and b N2 flow

3.3 Photoluminescence investigation

Figure 4 exhibits the photoluminescence (PL) spectra for CdS and Mg-doped CdS samples (air or N2) using 350 nm as an excitation wavelength. It can observe that the intensity of the PL emission of CdS sample prepared under N2 flows is higher than the intensity of CdS formed in air. Moreover, the PL intensity increased further in Mg doped sample prepared under nitrogen flow. It is recognized that the PL intensity and the ratio of the electron–hole recombination rate have a direct relation; as the recombination rate increased, the PL intensity enhanced. Furthermore, the PL intensity affected by the preparation’s technique and the kind of the doping elements. For example, Mg-doped ZnS formed applying hydrothermal procedure exhibited an improvement in the PL intensity owing to the creation of novel radiation centers (Heiba et al. 2015b). Furthermore, as Mg-doped nano CdSe, the PL intensity was enhanced as compared with the undoped one (Kwak et al. 2007). The emissions of undoped and doped samples (air and N2) can be analyzed applying Gaussian function into (violet, two blues), (violet, blue), (two violets, green) and (two violets) sub emissions, respectively. Previously, it was found that the CdS doped sample with 10%Mg, emitted violet and blue colors whether it produced in the air or underflow of N2 (Mohamed et al. 2018). CdS quantum dots formed using other techniques emitted almost similar or different emissions (López and Gómez 2014; Zhang et al. 2008; Mishra et al. 2011). For example; CdS quantum dots prepared using a post-synthesis modification technique with assist of NaOH solution exhibited violet-blue and orange-red emissions (López and Gómez 2014). When a ligand exchange method is applied to CdS, a strong violet color was observed (Zhang et al. 2008). CdS quantum dots produced by a Co-precipitation procedure emitted blue, green and yellow emissions (Mishra et al. 2011; Katari et al. 1994). Conversely, Mg-doped CdS–PVA emitted a green color (Krishnakumar et al. 2017). In addition, Mg-doped CdS formed using the co-precipitation technique exhibited green to red color owing to the imperfections in the samples (Mishra et al. 2011). The violet color can be ascribed to the band edge transition that resulted from the recombination of excitons or shallowly trapped electron–hole pairs (Krishnakumar et al. 2017). Alternatively, the blue emission caused by donor, acceptor and trap states (Lie et al. 2005).

Fig. 4
figure 4

a The photoluminescence measurements and b, c Gaussian fitting for Mg-doped CdS formed in air and N2 flow

3.4 Electronic analysis

In this study, the zinc-blende structure with F-43 m space group is used as a starting model for the undoped and Mg-doped CdS samples. In order to understand the behavior of the energy gap of doped samples as revealed in Fig. 3 (UV part), four cases were studied: Some Mg ions substituted some Cd ions in (i) stoichiometric CdS matrix, (ii) nonstoichiometric Cd1−δS (some cadmium deficiency), (iii) nonstoichiometric CdS1−δ (some sulfur deficiency) and (iv) non-stoichiometric CdS1−δOδ (some sulfur deficiency and some oxygen enter the empty space). The initial structure data was obtained from XRD study, Table 1. After geometry relaxation for undoped and doped samples in all previously mentioned cases. The calculated lattice parameter are 5.91, 5.83, 5.64, 5.73, 5.80 Å for CdS and Mg-doped (CdS, Cd1−δS, CdS1−δ and CdS1−δOδ) samples, respectively. The obtained lattice parameters from the current DFT calculation for CdS is smaller than the lattice parameters for the same material obtained previously from other theoretical studies for instance 6.0 Å or 5.976 Å (Jiang et al. 2016; Han et al. 2014) and it was somewhat different from the experimental result got from the current XRD study. The lattice parameter of Mg/CdS samples in this study is less than that of undoped sample, because of the different between the ionic radii of Mg and Cd ions as was investigated in detail in XRD part. The spin-polarized band structures for undoped and doped samples in the current study have been evaluated in first Brillouin zone through the high symmetry direction. The red dashed line in the band structure in Fig. 5 is standing for the Fermi energy level. Figure 5a revealed the band structure of CdS sample, where the sample exhibited a semiconductor characteristic with a direct band gap; the valence band maximum (VBM) and the conduction band minimum (CBM) situated at a similar high symmetry Г point. Moreover, the band gap value of CdS is 1.142 eV which in agreement with other previous theoretical studies (1.11 or 1.12 or 1.45 eV (Heyd et al. 2005; Noor et al. 2012; Deligoz et al. 2006)), but it difference from the results obtained from experimental part (2.58 and 2.12 eV for undoped sample annealed in air and N2, respectively, Fig. 3). The low value of the theoretical energy gap is predicted as the band gap is underestimation in DFT calculations as a result of the recognized limitation of expecting the precise energy band features (Heiba et al. 2020a). Figure 5 also shows that the Mg-doped CdS samples in all mentioned cases have direct band gap structure. Furthermore, all samples have a semiconductor nature except Mg-doped Cd1−δS, it has a metallic nature (Heiba et al. 2020b). The obtained theoretical energy gaps are 1.457, 1.913 and 1.051 eV for Mg-doped CdS, CdS1−δ and CdS1−δOδ samples, respectively. Where, the energy gap increased in all cases except the sample contained oxygen, the band gap energy decreased.

Fig. 5
figure 5

The electronic band structure of CdS and Mg-doped CdS (stoichiometric and non-stoichiometric)

Figure 6 reveals the density of states (DOS) with spin-up and down for CdS and Mg-doped CdS (in the current study). As shown from the figure that all samples in all cases are completely symmetrical, consequently all samples are non-magnetic. Figure 7 reveals the energy dependent of optical absorption for CdS and Mg-doped CdS (stoichiometric or non-stoichiometric). The obtained absorption spectra may result from the excitation of electrons from the valence to conduction band (Khan et al. 2016).

Fig. 6
figure 6

Total (DOS) and partial density of states (PDOS) of CdS and Mg-doped CdS (stoichiometric and non-stoichiometric)

Fig. 7
figure 7

The absorption spectra for CdS and Mg-doped CdS (stoichiometric and non-stoichiometric)

The absorption spectra for all studied cases exhibited an increasing trend up to specific energy value then it reduced. It is observed also that the absorption spectra of all Mg-doped samples are shifted toward the higher energy (blue shift) as compared with CdS sample except Mg-doped CdS1−δ sample. Where Mg-doped CdS1−δ sample revealed a redshift in the intermediate energy range and blue shift in the high energy range. The blue shift is pointed to the increase of the particle size and it is a signature of the formation of the nano structure sample, while the red shift may be caused by a reduction in the difference of energy levels upon applied electric field (Khan et al. 2019). Beyond the maximum of the absorption spectra, the absorption decreased for higher energy values. The extra peak appeared in the low energy range for Mg-doped CdS1−δ is due to the presence of Mg ions and vacancies together in CdS matrix, an analogous result was got in Mn-doped non-stoichiometric ZnS (Heiba et al. 2020b). Also the extra peak appeared in the visible range for Mg-doped CdS1−δ is due to the dramatic changes in the DOS of the sample as revealed in Fig. 5.

Figure 8a shows the change of refractive index (n) with energy for all samples. The index of refraction was rose upon increasing the photon energy (eV) until it reached its highest value, then it decreased in all samples except Mg-doped Cd1−δS, it reduced then enhanced until a specific limit then it decreased once more similar to the other samples. The change in the refractive index value may appear owing to the dispersion of the light deeper within the material. Therefore numerous interactions occurred inside the unit cell. The highest refractive indices value for CdS and Mg-doped CdS1−δ samples are 2.6 at 368 nm and 3.03 at 705 nm. It was found previously, the theoretical index of refraction for undoped sample is larger than the experiment result obtained from CdS thin film (Khan et al. 2019).

Fig. 8
figure 8

a The refractive index (n) and b extinction coefficient (k) for CdS and Mg-doped CdS (stoichiometric and non-stoichiometric)

As revealed from the Eq. (5), there is a correlation between α and k parameters, therefore a great similarity between the behaviors of α and k can be noticed. The photon energy dependence of the extinction coefficient was represented in Fig. 8b. The extinction coefficient results of the current samples gradually increased as the photon energy rose until it attained its highest value then it decreased with more increase in the photon energy. A tiny peak in the low energy range was appeared in Mg-doped Cd1−δS sample and another peak was observed in the visible range in the case of Mg-doped CdS1−δ sample. The appearance of these extra peaks may be owing to the localization of the density of states close to the Fermi level in Mg/Cd1−δS sample and the change of DOS in the case of Mg-doped CdS1−δ sample as shown in Fig. 5. Furthermore, the reduction in the intensity of the highest peak of CdS sample as it doped with Mg may be due to the interactions between different atoms.

Figure 9a reveals the real dielectric constant (ε1) as function of energy for all samples. The dielectric of CdS and Mg-doped (CdS and CdS1−δOδ) samples exhibited nearly a similar trend with a small shift but the other two samples (Mg-doped Cd1−δS or CdS1−δ) have different features. The dielectric of all samples may decrease or increase depending on the energy region. In addition, the real dielectric constant exhibited negative results in a definite energy range depend on kind of the sample. The negative results indicate that the reflected incident radiations displayed a metallic character as a result of the incompatibility of the samples bandgap with the energy of the incident wave (Saikia and Borah 2018). As revealed from the figure, the static dielectric constant has a maximum value in Mg-doped Cd1−δS sample and a minimum value in Mg-doped CdS sample. Figure 9b reveals the energy dependent of the imaginary part of the dielectric parameter for all samples. As demonstrated from the figure, undoped samples revealed three peaks. In addition, Mg-doped (CdS and CdS1−δOδ) samples have an analogous characteristic but the peak positions are changed relative to the undoped sample. These three peaks are emerged into two peaks in the case of Mg-doped CdS1−δ sample at 2.45 and 5.76 eV. Finally, Mg-doped Cd1−δS sample has also three peaks but at 5.77, 4.4 and 0.43 eV. Furthermore, the first peak of ε2 corresponded to the optical band gap, which is the threshold for optical transition between VBM and CBM (Deb and Sarkar 2021; Deb et al. 2020). The optical band gap of CdS, Mg-doped (CdS, Cd1−δS, CdS1−δ and CdS1−δOδ) samples are 2.1, 2.38, 2.5, 0.47 and 2.01 eV, respectively. Therefore, the reduction in the optical band gap of the Mg-doped CdS sample annealed in the air (UV part) is may be due to the existence of the oxygen inside the sample or existence of some sulfur deficiency in the sample.

Fig. 9
figure 9

a The real and b imaginary dielectric parts for CdS and Mg-doped CdS (stoichiometric and non-stoichiometric)

Figure 10 illustrates the optical conductivity (σ(ω)) of pure CdS together with Mg-doped samples in the current study. As demonstrated from the figure that the conductivity for all samples was increased with increased photon energy until it attained its highest value then it decreased. The well-defined peak appeared in UV region is due to interband transitions. On the other hand, in the ultraviolet region, the optical conductivity was decreased in all doped samples except Mg-doped CdS sample, it enhanced slightly. In the visible range, the photoconductivity was decreased except Mg-doped CdS1−δ sample is largely enhanced. Finally, Mg-doped Cd1−δS sample exhibited a photoconductivity response in the IR range.

Fig. 10
figure 10

The conductivity for undoped and Mg-doped CdS (stoichiometric and non-stoichiometric)

4 Conclusion

Rietveld refinement revealed that all XRD patterns of Cd0.95Mg0.05S sample formed in air and under nitrogen have two phases (cubic zinc blende and hexagonal wurtzite). The phase fractions of cubic and hexagonal phases are almost equal in the case of samples formed in the air, while for samples formed in the existence of nitrogen the hexagonal phase is dominant. The samples prepared under nitrogen gas are much better in crystallinity than prepared under air. The cell parameters and the z-coordinate of S ions were reduced upon doping the samples with Mg. FTIR confirmed the existence of Cd-S vibration band in all samples. Analysis of UV–Vis absorption spectra disclosed the optical band gap of CdS sample prepared in N2 is less relative to sample prepared in air. Also, upon doping with Mg, the optical band gap of CdS (air) sample decreased while it increased for CdS (N2) sample. The optical band gaps for 5%Mg-doped CdS (air) and 5%M -doped CdS (N2) samples are 1.95 and 2.35 eV, respectively. The red or blue shifts in the energy gaps may be owing to the size effect dependence, the percentage of cubic to hexagonal phase in CdS sample and the existence of some oxygen in the sample prepared air. The PL intensity of CdS sample was enhanced when it doped with Mg. The PL intensity of Mg-doped sample formed under nitrogen has a maximum value as compared with other samples. Undoped and Mg-doped CdS samples formed in air or N2 flow exhibited (violet, two blues), (violet, blue), (two violets, green) and (two violets) sub emissions, respectively. DFT showed that the energy gap increased in Mg-doped (CdS, Cd1−δS and CdS1−δ) while it decreased in Mg-doped CdS1−δOδ sample. Mg-doped CdS samples in all cases (CdS, CdS1−δ and CdS1−δOδ) have the direct band gap structure and non-magnetic nature. Mg-doped (CdS, CdS1−δ and CdS1−δOδ) samples have a semiconductor nature while Mg-doped Cd1−δS has a metallic nature. Mg-doped CdS1−δ has the highest refractive index. The photoconductivity of Mg-doped CdS1−δ is largely enhanced in the visible range while Mg-doped Cd1−δS sample exhibited a photoconductivity response in the IR range.