Run the following code, which is in the chapter07/optocoupler_test.py file. The relay should activate with a click sound, and deactivate after 2 seconds. This is the same code we used when we created and tested our optocoupler circuit since it's the optocoupler that our Raspberry Pi is connected to.
We saw earlier when we learned about MOSFETs that we could connect the MOSFET directly to a GPIO pin and control the relay, without needing an optoisolator. So, why does the preceding circuit have one?
The answer is that our circuit does not technically need one, and there are ready-made relay modules to be found (though rarer) that do not have an optoisolator. However, there is no harm in having one present since it does provide a level of electrical isolation protection just in case the relay control circuit fails or there is a mishap when wiring up the power supply.
Finally, what about relay modules you can find on sites such...