Do I need license for the Arduino code that a company has posted in their websites when I buy their sensors to use them to my Arduino? For instance, I buy a CO2 sensor and I use the company's code in order to make this CO2 sensor work with my Arduino. Do I need license if I want to patent a machine using the latter sensor?
Unknown based on what you have posted in your question. But answer lies in the terms under which the source code is made available or licensed.
The most common scenario is that such "sample" source code is provided as help to the purchaser of their product and you'll often see things that permit its use in conjunction with the hardware product. You'll also often see disclaimers that say the code is for demonstration purposes only and it not warrantied to do anything at all.
But again, you'll need to review the actual language under which the code is provided and then understand and follow its terms and conditions.